echoing an array value giving a key from another array - php

I know this is simple, but cant get my head around it...
I have 2 arrays. Both populated from a database lookup.
Array 1
Array (
[sailID] => 7
[sailTag] => 100004
[assigneduser] => Jason Ellmers
[assigneddate] => 2018-05-30 17:48:57
[cutuser] => Jason Ellmers
[cutdate] => 2018-05-30 20:31:23
[stickuser] => Jason Ellmers
[stickdate] => 2018-05-30 20:38:24
[corneruser] => Jason Ellmers
[cornerdate] => 2018-05-30 20:38:54
[finishuser] => Jason Ellmers
[finishdate] => 2018-05-30 20:39:53
[checkuser] =>
[checkdate] => 0000-00-00 00:00:00
[DesignRef] => 420abcdefg
[OrderingLoft] => 1
[ClassRef] => 1
[ClothType] => Bainbridge
[ClothColour] => White
[ClothWeight] => 12oz
[SailNo] => GB342398 )
Array 2
Array (
[0] => Array (
[id] => 1
[name] => 420 )
[1] => Array (
[id] => 2
[name] => J24 ) )
What I am after doing is being able to echo to the screen $array1['Where the ClassRef is a lookup of the ID in Array2' and displays the Name from Array2]
So for the above example the Echo would be '420'
I think I could do it using a foreach or while loop but that seems a bit cumbersome???

I've had to put some test data together, but from the comment, the idea is to re-index the second array using array_column() with the id as the index, so the code (as you've worked out) is...
$array1 =[
"sailID" => 7,
"sailTag" => "100004",
"ClassRef" => 1 ];
$array2 = [["id" => 1, "name" => "420"],
["id" => 2, "name" => "J24"]];
$array2 = array_column($array2, "name", "id");
echo $array2[$array1["ClassRef"]];

Related

merge two equal structural multi-arrays with different data

i can't believe that i can't find a solution to this seemingly simple problem, and i tried everything.
i have two multi arrays:
the first one:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
)
the second one has the exact same structure, but with new data:
Array
(
[3] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
what i need mergin both multi-arrays is this result:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
[final_approved] =>
[final_id_note] => 19
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
)
but i can't get this result with any common php function:
array_merge:
Array
(
[0] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[1] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
[2] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
it replaces with new indexes.
array_push:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
[5] => Array
(
[3] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
)
it literally pushes in with the second array with new indexes, which is correct of course lol
i guess that the answer is pretty simple, but i already googled and try everything that my newbie ability can.
can anyone give me some advice?
thanks!
There are recursive array functions in PHP. array_merge_recursive, despite the promising name, will not work here, as it will reindex the numeric keys and add overlapping the data to the end.
However, there is array_replace_recursive that does exactly what you want when you have a multidimensional array with significant keys. Here's your data (in a usable format!):
// The base data:
$a_one = [
3 => [
1 => [
'approved' => 1,
'id_note' => 1,
'surname' => 'Rawson',
],
2 => [
'approved' => 0,
'id_note' => 18,
'surname' => 'Vallejo',
],
],
4 => [
1 => [
'school_id' => 1,
],
2 => [
'available' => 1,
],
],
];
// Data to be added by matching keys:
$a_two = [
3 => [
1 => [
'final_approved' => 1,
'final_id_note' => 1,
],
2 => [
'final_approved' => 0,
'final_id_note' => 19,
],
],
];
Then let's mash them together:
$merged = array_replace_recursive($a_one, $a_two);
This results in the following "merged" array:
[
3 => [
1 => [
'approved' => 1,
'id_note' => 1,
'surname' => 'Rawson',
'final_approved' => 1,
'final_id_note' => 1,
],
2 => [
'approved' => 0,
'id_note' => 18,
'surname' => 'Vallejo',
'final_approved' => 0,
'final_id_note' => 19,
],
],
4 => [
1 => [
'school_id' => 1,
],
2 => [
'available' => 1,
],
],
]
In summary, the way this function behaves (from the manual, numbering added):
If a key from the first array exists in the second array, its value will be replaced by the value from the second array.
If the key exists in the second array, and not the first, it will be created in the first array.
If a key only exists in the first array, it will be left as is.
Or in a more compact statement:
key: scalar EXISTS in A, B => REPLACE from B to A
key: scalar EXISTS in B => CREATE in A
key: scalar EXISTS in A => LEAVE in A
Again, the manual says:
When the value in the first array is scalar, it will be replaced by the value in the second array, may it be scalar or array. When the value in the first array and the second array are both arrays, array_replace_recursive() will replace their respective value recursively.
What's important to note here is that array members with matching keys, that are arrays, will not be "replaced" in full. The "replacement" only applies to the final scalar values or "leaves" of the array (per the logic above). The arrays will be "merged", ie. non-matching keys with scalar values will be combined within each "final array" (or "leafy array" for lack of a better term).
N.B. If you're using this function to "merge" more complex arrays, you'll want to double-check that the outcome matches your expectations. I can't remember off the top of my head all the "quirks" that exist in using this function, but I assure you they are there... don't assume that it will create a seamless union of any and all datasets you may throw at it.

Echo value from PHP array when brackets in key-value pairs

Quick question, how do you echo a variable when the array has key's that contain names with brackets etc. See array below:
[data] => Array
(
[0] => stdClass Object
(
[id] => 4
[contact_id] =>
[status] => Complete
[is_test_data] => 0
[datesubmitted] => 2014-04-16 22:18:39
[sResponseComment] =>
[responseID] => 4
[[question(1), option(0)]] =>
[[question(2)]] => John
[[question(3)]] => Wtf#wtf.com
[[question(5)]] => 0975735289
[[question(6)]] => 3010
[[question(9), option(0)]] =>
[[question(10)]] => Testing
[[question(11)]] => Later
[[question(12)]] => This year
)
Lets say i want to echo the answer to question 2, which is "John". How do i go about doing this?
Well you have an array conataining an array of objects:
echo $array['data'][0]->{'[question(2)]'};

PHP: array's internal pointer stops one element before last one - weird

I have a simple multidimensional array. I modify it - ie. I add data to it - in a foreach loop, accessing its elements by reference:
$array = array(
array('id' => 12, 'name' => 'John1', 'surname' => 'Smith1'),
array('id' => 13, 'name' => 'John2', 'surname' => 'Smith2'),
array('id' => 14, 'name' => 'John3', 'surname' => 'Smith3'),
array('id' => 15, 'name' => 'John4', 'surname' => 'Smith4'),
);
foreach($array as &$a) {
$a['middlename'] = 'Robert';
}
Now what's below shows that $array is perfectly in order:
print('<pre>'.print_r($array,true).'</pre>'); results in:
Array
(
[0] => Array
(
[id] => 12
[name] => John1
[surname] => Smith1
[middlename] => Robert
)
[1] => Array
(
[id] => 13
[name] => John2
[surname] => Smith2
[middlename] => Robert
)
[2] => Array
(
[id] => 14
[name] => John3
[surname] => Smith3
[middlename] => Robert
)
[3] => Array
(
[id] => 15
[name] => John4
[surname] => Smith4
[middlename] => Robert
)
)
But while I loop though it, internal pointer stops right before last element:
foreach($array as $a) {
print('<pre>'.print_r($a['id'],true).'</pre>');
}
outputs:
12
13
14
14
Any hints what's going on?
UPDATE: The answer I picked is correct and moreover I found this: PHP Pass by reference in foreach
Thx, SO.
I've had a similar issue, when modifying an array with a foreach and then accessing the array afterwards. My guess is that you are not destroying the reference after you loop through, and are overwriting the value accidentally. Try unset($a); after your foreach.
I would suggest not to use variable names like $array, just in case...

How To push array elements from one array to another array

Hello people here is the code that i was using initially....
array_push($Parameter_IdArray, $Parameter_Id1, $Parameter_Id2, $Parameter_Id3, $OptParameter_Id);
array_push($Eqt_ParamArray, $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
i had no issues to push array values .... but now $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 are in one more array it is something like this
Array ( [Profile_Id] => 4 [eqt_param] => Array ( [0] => 4.00 [1] => 4.00 [2] => 4.00 ) [Parameter_Id1] => 8 [min_param] => Array ( [0] => 1.00 [1] => 1.00 [2] => 1.00 ) [max_param] => Array ( [0] => 5.00 [1] => 5.00 [2] => 5.00 ) [Wtg_param] => Array ( [0] => 25.00 [1] => 25.00 [2] => 50.00 ) [Parameter_Id2] => 5 [Parameter_Id3] => 1 [Opt_eqt_param] => Array ( [0] => 0.00 ) [OptParameter_Id] => 14 [Opt_wtg] => Array ( [0] => 1.05 ) [operator] => Array ( [0] => M ) [eqt_pay] => 1,574,235 [rec_sal] => 1,485,000 [#] => -6.01 % [Emp_Id] => 490699 [Emp_Process] => Confirm New Pay )
now i need tp push array values $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 to $Eqt_ParamArray how to do that?
If I understand correctly, you've now got an associative array in PHP. On that assumption, I went ahead and reformatted the nightmare for you:
$myarray = array(
"Profile_Id" => 4,
"eqt_param" => array(4.00, 4.00, 4.00),
"Parameter_Id1" => 8,
"min_param" => array (1.00, 1.00, 1.00 ),
"max_param" => array (5.00, 5.00, 5.00 ),
"Wtg_param" => array (25.00, 25.00, 50.00 ),
"Parameter_Id2" => 5,
"Parameter_Id3" => 1,
"Opt_eqt_param" => array (0.00),
"OptParameter_Id" => 14,
"Opt_wtg" => array (1.05),
"operator" => array ("M"),
"eqt_pay" => array(1,574,235),
"rec_sal" => array(1,485,000),
"#" => "-6.01 %",
"Emp_Id" => 490699,
"Emp_Process" => "Confirm New Pay"
);
What this good formatting now makes clear is that you can use array_push directly on the "eqt_param" index:
array_push($myArray["eqt_param"], $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
You may also mean that you want to replace it, which is easy too:
$myArray['eqt_param'] = array($eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
The same principles apply in Javascript, which you have had tagged so maybe you mean that.

how can i access such a php array

i was trying to access this php array with no luck, i want to access the [icon] => icon.png
Array ( [total] => 2
[total_grouped] => 2
[notifys] => Array ( [0] => Array ( [notifytype_id] => 12
[grouped] => 930
[icon] => icon.png
[n_url] => wall_action.php?id=930
[desc] => 690706096
[text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
$arr['notifys'][0]['icon']
ETA: I'm not sure what your comment means, the following code:
$arr = array('total'=>2, 'total_grouped' => 2, 'notifys' => array(array(
'notifytype_id' => 12, 'icon' => 'icon.png')));
echo '<pre>';
print_r($arr);
echo '</pre>';
var_dump($arr['notifys'][0]['icon']);
outputs:
Array
(
[total] => 2
[total_grouped] => 2
[notifys] => Array
(
[0] => Array
(
[notifytype_id] => 12
[icon] => icon.png
)
)
)
string(8) "icon.png"
Generally, code never outputs nothing. You should be developing with all errors and notifications on.
$arr['notifys'][0]['icon']
rg = Array ( [total] => 2 [total_grouped] => 2 [notifys] => Array ( [0] => Array ( [notifytype_id] => 12 [grouped] => 930 [icon] => icon.png [n_url] => wall_action.php?id=930 [desc] => 690706096 [text] => Array ( [0] => Sarah O'conner ) [total] => 1 )));
icon = rg["notifsys"][0]["icon"];
Everybody is posting right answer. Its just you have giving a wrong deceleration of array.
Try var_dump/print_r of array and then you can easily understand nodes.
$arr = array(total => 2,
total_grouped => 2,
notifys => array( 0 => array(notifytype_id => 12,
grouped => 930,
icon => 'icon.png',
n_url => 'wall_action.php?id=930',
desc => 690706096,
text =>array(0 => 'Sarah Oconner' ),
total => 1,
),
),
);
echo $arr['notifys']['0']['icon'];

Categories