Not getting array all values using php - php

I have this following array
$question = array(
"ques_15" => array(
"name" => array(
"0" => "aaa"
)
),
"ques_16" => array(
"name" => array(
"0" => "bbb",
"1" => "ccc"
)
)
);
$i=0;
foreach($question as $k=>$v)
{
echo $question[$k]['name'][$i];
$i++;
}
But my output is only
aaaccc
I am missing the value bbb

You need to iterate the inner 'name' arrays - you could use a nested foreach loop:
$question = array(
"ques_15" => array(
"name" => array(
"0" => "aaa"
)
),
"ques_16" => array(
"name" => array(
"0" => "bbb",
"1" => "ccc"
)
)
);
foreach($question as $quest)
{
foreach($quest['name'] as $val)
{
echo $val;
}
}

you should loop though like so
foreach($question as $q)
{
foreach($q['name'] as $v)
{
echo $v;
}
}

in foreach you don't need a counter $i, it's for while() or for()
you array is two dimensional so you need 2 foreach

Check it out in a functional way.
The shorthand array declaration works only on PHP 5.4+ though, but it still works with your longhand array declaration.
$questions = [
'ques_15' => ['name' => ['aaa']],
'ques_16' => ['name' => ['bbb', 'ccc']]
];
array_map(function($a){
foreach ($a['name'] as $v) echo $v;
}, $questions);

Related

Assign values in array based on array keys

How to modify an array based on the value as key?
array(
array(
"name" => "BIBAR",
"cutoff" => 20220725,
"totals" => 5614
),
array(
"name" => "BIBAR",
"cutoff" => 20220810,
"totals" => 5614
),
array(
"name" => "BIBAR",
"cutoff" => 20220825,
"totals" => 5614
)
);
I tried the following but it's not working:
foreach($cutoffs as $catoff) {
$ii = 0;
$sums[$ii][$catoff] = array_filter($array, function($val){
return $val['cutoff'] === $catoff ? $val['totals'] : $val;
});
$ii++;
}
My desired array:
array(
'20221025' => array(
12345,
12343,
24442
),
'20221110' => array(
3443,
744334
)
)
I'm stuck here for hours ... Please help
IF the "name" is irrelevant, I think also the previous answer should be fine.
If this code does "not work", then your explanation might be wrong, so you need to either explain better, or give us more examples - please mind that in your example the input and output are very different - the input you gave does not match your ouput.
My code is:
$a = array(
array(
"name" => "BIBAR",
"cutoff" => 20220725,
"totals" => 5614
),
array(
"name" => "BIBAR",
"cutoff" => 20220810,
"totals" => 5614
),
array(
"name" => "BIBAR",
"cutoff" => 20220725,
"totals" => 1234
)
);
print_r($a);
echo "\n================================\n\n";
$newArr = [];
foreach ($a as $k => $vArr) {
// maybe some validation would be useful here, check if they keys exist
$newArr[$vArr['cutoff']][] = $vArr['totals'];
}
print_r($newArr);
function changeArr($data){
$new = [];
foreach ($data as $v){
$new[$v['cutoff']][] = $v['totals'];
}
return $new;
}

Compare Dynamic Multidimensional Arrays

I have two multidimensional dynamic arrays like so in PHP:
$old = array(
"meta_data" => array(
"page_title" => "Test1",
"page_description" => "Test2",
"page_keywords" => "Test3"
),
"content" => array(
"page_header_one" => "1",
"page_content_one" => "2",
"page_header_two" => "3",
"page_content_two" => "4",
"page_header_three" => "5"
),
);
$new = array(
"meta_data" => array(
"page_title" => "Test1",
"page_description" => "Test2",
"page_keywords" => "Test3324"
),
"content" => array(
"page_header_one" => "124",
"page_content_one" => "243",
"page_header_two" => "343"
),
);
I'm struggling to compare these as they're dynamic, e.g. the keys change. What I'm aiming to do is compare the arrays, find out what's changed, leave out keys that don't match, and only add the changes to the new array.
The only things that will be constant are "meta_data" and "content"
So for example in $old we have 5 items in the content array, but in the $new array we only have 3 items (3 changed items), so the new array would have 3 content items.
Is there a way to do this I can't for the life in me figure out how?
Final array should look like so:
$final = array(
"meta_data" => array(
"page_keywords" => "Test3324"
),
"content" => array(
"page_header_one" => "124",
"page_content_one" => "243",
"page_header_two" => "343"
),
);
Maybe it can be done easier, but I think this will do the job:
<?php
$newArray = [];
foreach($old['meta_data'] as $key => $value) {
if(array_key_exists($key, $new['meta_data'])) {
if($new['meta_data'][$key] == $old['meta_data'][$key]) {
$newArray['meta_data'][$key] = $value;
}
}
}
foreach($old['content'] as $key => $value) {
if(array_key_exists($key, $new['content'])) {
if($new['content'][$key] == $old['content'][$key]) {
$newArray['content'][$key] = $value;
}
}
}
You can also make the top key dynamic, so content and meta_data are dynamic as well, like this:
foreach($old as $topKey => $subArray) {
if(array_key_exists($topKey, $new)) {
foreach($subArray as $key => $value) {
if(array_key_exists($key, $new[$topKey])) {
if($new[$topKey][$key] == $old[$topKey][$key]) {
$newArray[$topKey][$key] = $value;
}
}
}
}
}
I haven't tested it. But I think you'll get the gist.
The only things that will be constant are "meta_data" and "content"
Given this thing in mind, the solution to your problem would be to use array_diff() function like this:
$final = array();
$final['meta_data'] = array_diff($new['meta_data'], $old['meta_data']);
$final['content'] = array_diff($new['content'], $old['content']);
// display $final array
var_dump($final);
Here's a live demo
You can simply use the PHP array_diff() function.
$final = array_diff($new, $old);

PHP multidimensional array search key by value

For example a multidimensional array like an example below
$arr = array(
[H1] => array(
"name" => "A"
"title" => "T1"
)
[H2] => array(
"name" => "B"
"title" => "B1"
)
)
Let's say I would like to search name which equals to A in $arr and if it's matched, the searching should return the key which is H1
How can I do that in php ?
I tried array_keys($arr, "A") but it returns me with an array instead of the key.
This may help -
$arr = array(
'H1' => array(
"name" => "A",
"title" => "T1",
),
'H2' => array(
"name" => "B",
"title" => "B1",
)
);
// Generate a new array with 'keys' and values in 'name'
$new = array_combine(array_keys($arr), array_column($arr, 'name'));
// Search in that new array
$search = array_search('A', $new);
var_dump($search);
Output
string(2) "H1"
Demo
Another simple way would be -
$serach= false;
foreach($arr as $key => $val) {
if($val['name'] == 'A') {
$search= $key;
break;
}
}
var_dump($search);

Getting error T_DOUBLE_ARROW for multi dimensional associative array

I am testing out a recursive PHP script. But the associative array beginning with "items" key is throwing back the error T_DOUBLE_ARROW. Do I need to use brackets for this portion? The "methods" key is a layer within the "items" keys. Can someone guide me to how I can fix this error? The find_in_arr function works fine when calling name, subject and type keys. But when it gets to items I get the error.
<?php
function find_in_arr($key, $arr)
{
foreach ($arr as $k => $v)
{
if ($k == $key)
{
return $v;
}
if (is_array($v))
{
foreach ($v as $_k => $_v)
{
if ($_k == $key)
{
return $_v;
}
}
}
}
return false;
}
$arr =
array(
"name" => "Php Master",
"subject" => "Php",
"type" => "Articles",
"items" => ("one" => "Iteration","two" => "Recursion",
"methods" => ("factorial" => "Recursion","fibonacci" => "Recursion"),)
"parent"? => "Larry Ullman",
);
var_dump
(
find_in_arr('two', $arr),
find_in_arr('parent', $arr),
find_in_arr('fibonacci', $arr)
//find_in_arr('name', $arr),
//find_in_arr('subject', $arr),
//find_in_arr('type', $arr)
);
It should be:
$arr = array(
"name" => "Php Master",
"subject" => "Php",
"type" => "Articles",
"items" => array(
"one" => "Iteration",
"two" => "Recursion",
"methods" => array(
"factorial" => "Recursion",
"fibonacci" => "Recursion"
)
),
"parent" => "Larry Ullman"
);

Put array elements with specific value to end of array

I have a multidimensional array like that:
$array = array(
1 => array(
"name" => 'Jon',
"year" => '2012'
),
2 => array(
"name" => 'Jack',
"year" => '1900'
),
3 => array(
"name" => 'Lisa',
"year" => '1900'
),
4 => array(
"name" => 'Ygritte',
"year" => '1929'
),
);
All the items that have a year of '1900' should be put to the end of the array. What is a lightweight solution?
Desired result:
$array = array(
1 => array(
"name" => 'Jon',
"year" => '2012'
),
2 => array(
"name" => 'Ygritte',
"year" => '1929'
),
3 => array(
"name" => 'Jack',
"year" => '1900'
),
4 => array(
"name" => 'Lisa',
"year" => '1900'
),
);
I assume you don't care about your indexes because you rearrange them. First thing, you should probably start indexes in your array from 0 and not for 1.
You can use the following code:
$array = array(
1 => array(
"name" => 'Jon',
"year" => '2012'
),
2 => array(
"name" => 'Jack',
"year" => '1900'
),
3 => array(
"name" => 'Lisa',
"year" => '1900'
),
4 => array(
"name" => 'Ygritte',
"year" => '1929'
),
);
$array = array_values($array);
for ($i=0, $c = count($array); $i<$c; ++$i) {
if ($array[$i]['year'] == '1900') {
$array[] = $array[$i];
unset($array[$i]);
}
}
$array = array_values($array);
foreach ($array as $k => $v) {
echo $k.' '.$v['name'].' '.$v['year']."<br />";
}
Result for this is:
0 Jon 2012
1 Ygritte 1929
2 Jack 1900
3 Lisa 1900
Of course if you want, you can change your keys adding 1 to each one (starting from the last element) so you have the same output as in question but I assume this one is enough for you.
The simplest approach could be something like this:
Find the element that has year 1900 (iterating the loop) then,
For example working for index 3
$tmp = $myArray['3'];
unset($myArray['3'];
$myArray['3'] = $tmp;
$tmp is a temporary variable inside the loop.
Try using SplHeap
class MyQueue extends SplHeap
{
public function compare($item1, $item2)
{
if ($item1['year'] === $item2['year']) return 0;
return ($item1['year'] == 1900) ? -1 : 0;
}
}
$queue = new MyQueue();
foreach ($array as $item) {
$queue->insert($item);
}
$queue->top();
$array = array();
while($queue->valid()){
$array[] = $queue->current();
$queue->next();
}
print_r($array);
Hope this following code will help you:
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
rsort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
aasort($array,"year");
print_r($array);

Categories