简体   繁体   中英

PHP how to Push an array to a value in another array (multidimensional array)

I have been trying to code this for a while but I am so confused with multidimensional arrays.

I have simplified it to this:

Consider I already have an array:

$myarray = ("1233","4345","3452");

I would like to push an array, onto this array (to create multidimensional array) at a certain value.

For example, With these two variables:

$eventID = 4345
$photoID = 12

I would want to end up with:

("1233", "4345 => 12", "3452")

First, your array needs to be an "associative array". You can create the array in one statement like this:

$myarray = array(
    1233 => "",
    4345 => 12,
    3452 => ""
);

Or one item at a time like this:

$myarray = array();
$myarray[1233] = "";
$myarray[4345] = 12;
$myarray[3452] = "";

This is still one dimensional array. You can go one step further and create a multi-dimensional array like this:

$myarray = array();
$myarray[1233] = "";
$myarray[4345] = array("PhotoID" => 12, "Location" => "Somewhere");
$myarray[3452] = array();
$myarray[3452]["PhotoID"] = 13;
$myarray[3452]["Location"] = "Somewhere else";

See the section on Arrays in PHP manual. It is a very frequently used data structure in PHP and I encourage you to read the section thoroughly.

In PHP arrays works like that:

$a = array("1233", "4345", "3452");

In the above example, the values, "1233", "4345" and "3452" they have each own an index number. So if you run that code:

$a = array("1233", "4345", "3452");

echo "<pre>";
print_r($a);
echo "</pre>";

you will get that result:

Array
(
    [0] => 1233
    [1] => 4345
    [2] => 3452
)

In that case you can't assign an array on "4345" but on "[1]". So, with that in mind if you have another one array like that :

$b = array("321", "654", "987");

and you like to assign it into position "[1]" then you have to do something like that:

$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");

$a[1] = $b;

TAKE CARE

The above code will replace your value "4345" with the content of the array $b. Now let's try to print out your array:

$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");

$a[1] = $b;

echo "<pre>";
print_r($a);
echo "</pre>";

The result will be that now:

Array
(
    [0] => 1233
    [1] => Array
        (
            [0] => 321
            [1] => 654
            [2] => 987
        )

    [2] => 3452
)

Finaly, if you like to keep both the values "4345" from the array $a and assign an array to that same position into the array $a you have to consider what you like to do with the value "4345"

Some ideas are here:

$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");

$b[] = $a[1];
$a[1] = $b;

echo "<pre>";
print_r($a);
echo "</pre>";

The above code has that result:

Array
(
    [0] => 1233
    [1] => Array
        (
            [0] => 321
            [1] => 654
            [2] => 987
            [3] => 4345
        )

    [2] => 3452
)

Or you can try that:

$a = array("1233", "4345", "3452");
$b = array("321", "654", "987");
$c = array();

$c[] = $a[1];
$c[] = $b; 
$a[1] = $c;

echo "<pre>";
print_r($a);
echo "</pre>";

The above code will have the following result

Array
(
    [0] => 1233
    [1] => Array
        (
            [0] => 4345
            [1] => Array
                (
                    [0] => 321
                    [1] => 654
                    [2] => 987
                )

        )

    [2] => 3452
)

It looks like you want to create an associative array from an existing array. You can try this:

$assoc_array = array_combine($myarray, array_fill(0, count($myarray), null));
$assoc_array['4345'] = 12;

$assoc_array will be filled with null values for all other eventIDs.

This code will produce what you want:

$myarray = array("1233","4345","3452");
print_r($myarray);
$eventID = 4345;
$photoID = 12;

if( in_array( $eventID, $myarray) )
{
    array_diff( $myarray, array($eventID) );
    $myarray[ $eventID] = $photoID;
}

print_r($myarray);

Do you want to create a key value pair? In that case why don't u try

$myarray["4345"]=12;

which will end up and array like

Array ( [0] => 1233 [1] => 4345 [2] => 3452 [4345] => 12 ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM