foreach loop is not working after print array - php

I've tried to loop of following array.
Array
(
[mech_info] => Array
(
[make] => Amaka
[0] => Array
(
[year] => 2001
[model] => Array
(
[0] => Test one
[1] => test fix
[2] => Hamour
[3] => Imagica
)
)
[1] => Array
(
[year] => 2002
[model] => Array
(
[0] => Test Two
)
)
[2] => Array
(
[year] => 2014
[model] => Array
(
[0] => Test three
)
)
[3] => Array
(
[year] => 2015
[model] => Array
(
[0] => test four
)
)
)
)
Array
(
[mech_info] => Array
(
[make] => PRI
[0] => Array
(
[year] => 2005
[model] => Array
(
[0] => PRIMODE
[1] => Temp Pri
[2] => primode
[3] => yyy
)
)
)
)
I want to do it with foreach loop. I have tried by following code but it is not show anything except
`print_r($_POST['mech_show']);`.
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model_data as $key => $mec_value) {
echo "string";
echo $meta_value['make'];
}
echo "<pre>";
print_r($_POST['mech_show']);
exit();
also not able to go under foreach and data not print in loop.
given me error
Notice: Undefined index: mech_info
Warning: Invalid argument supplied for foreach() in
i also trie this way but
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $_POST['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
echo "<pre>";
print_r($all_make_model['mech_info']);
but it's showing Warning: Illegal string offset 'mech_info' in ..
I don't know if my code is wrong or I'm missing something anyone pls help me.
Thank You

Some change your foreach loop. it $meta_value['make'] should be $mec_value['make']
So,
$all_make_model= $_POST['mech_show'];
//$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}

try
$all_array=array("mech_info"=>array("make"=>"Amaka",array("year"=>2001,"model"=>array("one","two","three")),array("year"=>2002,"model"=>array("one","two","three")),array("year"=>2003,"model"=>array("one","two","three")),array("year"=>2004,"model"=>array("one","two","three"))),array("mech_info"=>array("make"=>"PRI",array("year"=>2001,"model"=>array("one","two","three")))));
$all_make_model= $all_array;
//$all_make_model_data = $all_make_model['mech_info'];
//print_r($all_make_model['mech_info']);
foreach ($all_make_model['mech_info'] as $key => $mec_value) {
if(is_numeric($key)) continue;
echo $mec_value; // output Amaka
}
exit();

This Code just work.
To iterate on multiple mech_info, i added a leave in the array, because otherwise you are tring to create multiple object with the same index.
$p = Array('mech_show' => Array(
0 => Array(
'mech_info' => Array(
'make' => 'Amaka',
'0' => Array(
'year' => 2001,
'model' => Array(
0 => 'Test one',
1 => 'test fix',
2 => 'Hamour',
3 => 'Imagica'
)
),
'1' => Array(
'year' => 2002,
'model' => Array(
0 => 'Test Two'
)
),
'2' => Array(
'year' => 2014,
'model' => Array(
0 => 'Test three'
)
),
'3' => Array(
'year' => 2015,
'model' => Array
(
0 => 'test four'
)
)
)
),
1=>Array(
'mech_info' => Array(
'make' => 'PRI',
'0' => Array(
'year' => 2005,
'model' => Array(
0 => 'PRIMODE',
1 => 'Temp Pri',
2 => 'primode',
3 => 'yyy'
)
)
)
)
)
);
$all_make_model= $p['mech_show'];
foreach($all_make_model as $all_make_model_data){
foreach($all_make_model_data as $mech_info)
var_dump($mech_info['make']);
}
where you have to replace $p with $_POST

Related

Convert 2d array by specified 2d format

I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,
You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.
You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);

How do I get values from this array? PHP

Very much new to PHP and completely stuck because of #attributes when trying to pick values out of an array. I have previously converted an xml file into a associative array.
print_r(array_keys($newArray));
Prints:
Array ( [0] => #attributes [1] => rate )
print_r ($newArray);
Prints:
Array (
[#attributes] => Array (
[base] => GBP
[ts] => 1572881347 )
[rate] => Array (
[0] => Array (
[#attributes] => Array (
[code] => AED
[rate] => 4.739532203531
[live] => 0
)
)
[1] => Array (
[#attributes] => Array (
[code] => AFN
[rate] => 100.80144337913
[live] => 0
)
)
[2] => Array (
[#attributes] => Array (
[code] => ALL
[rate] => 142.29721877018
[live] => 0
)
)
[3] => Array (
[#attributes] => Array (
[code] => AMD
[rate] => 614.98298701028
[live] => 0
)
)
How would I specify and print the last value?
[3] => Array ( [#attributes] => Array ( [code] => AMD [rate] => 614.98298701028 [live] => 0.
With the goal to output something like: AMD, 614.98298701028, 0
Thanks
More generally, if you'd want to look-up a currency in your array, you could use following recursive function:
$code = 'AMD'; // set the curncy you want to lookup
forex($code, $newArray); // invoke the recursive function
// output:
//code : AMD
//rate : 614.98298701028
function forex(string $code = '', array $arr = []): void
{
foreach ($arr as $key => $value) {
if (is_array($value) && $key !== '#attributes') {
forex($code, $value);
} else {
if (isset($value['code']) && $value['code'] == $code) {
echo 'code : ' . $value['code'];
echo '<br />';
echo 'rate : ' . $value['rate'];
echo '<br />';
}
}
}
}
working demo
Assuming that your original array is something like this:
$a = array(
'#attributes' => array(
'base' => 'GBP',
'ts' => 1572881347),
'rate' => array(
0 => array(
'#attributes' => array(
'code' => 'AED',
'rate' => 4.739532203531,
'live' => 0,
),
),
1 => array(
'#attributes' => array(
'code' => 'AFN',
'rate' => 100.80144337913,
'live' => 0,
),
),
2 => array(
'#attributes' => array(
'code' => 'ALL',
'rate' => 142.29721877018,
'live' => 0,
),
),
3 => array(
'#attributes' => array(
'code' => 'AMD',
'rate' => 614.98298701028,
'live' => 0,
),
)));
then you can get what you want with this:
$last_rate = sizeof($a['rate']) - 1;
foreach ($a['rate'][$last_rate]['#attributes'] as $key => $value) {
echo "$value ";
}
Which will output:
AMD 614.98298701028 0
If your array will always contain the array values under rate, you can retrieve the last element of the rate array by using end($newArray['rate']).
To output a CSV style string you can simply use implode(', ', $last['#attributes']) or another desired method, such as foreach, to display the values from the last element.
Example: https://3v4l.org/FNuuZ
if ($last = end($newArray['rate'])) {
echo implode(', ', $last['#attributes']);
}
Result:
AMD, 614.98298701028, 0

PHP array merge based on key & value

I have one array and try to change some key and value for example if sku are same than i need to merge image. Below array i have
Array
(
[0] => Array
(
[sku] => h-eldora
[name] => H ELDORA
[image] => s/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054
)
[1] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
)
[2] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[3] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
)
[4] => Array
(
[sku] => hl-dracy
[name] =>
[image] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
)
[5] => Array
(
[sku] => hl-dracy
[name] =>
[image] =>s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
I need to merge array like this
Array
(
[0] => Array
(
[sku] => h-eldora
[name] =>
[image1] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
[image2] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
[image3] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[1] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image1] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
[image2] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
[image3] => s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
If any php function is there than please let me know or any code suggestion
Using simple PHP:
<?php
$arr1 = array(
0 => array(
'sku' => 'h-eldora',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054'
),
1 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221'
),
2 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598'
),
3 => array(
'sku' => 'hl-dracy',
'name' => 'HL DRACY',
'image' => 's/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222'
),
4 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223'
),
5 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793'
)
);
$newArr = $imgIndex = array();
foreach($arr1 as $a){
if( !array_key_exists($a['sku'],$newArr) ){
$newArr[$a['sku']] = array(
'sku' => $a['sku'],
'name' => $a['name'],
'image1' => $a['image']
);
$imgFound[$a['sku']] = 1;
}else{
$imgFound[$a['sku']]++;
$newArr[$a['sku']]['image'.$imgFound[$a['sku']]] = $a['image'];
}
}
unset($imgFound);
echo '<pre>'; print_r($newArr); echo '</pre>';
?>
This could work for you:
$data = // your input array
$uniqueSKUs = Array();
$newArray = Array();
$currentIndex = -1;
foreach ($data as $item) {
if (!in_array($item['sku'], $uniqueSKUs)) {
$currentIndex++;
$uniqueSKUs[] = $item['sku'];
$newArray[$currentIndex] = Array(
'sku' => $item['sku'],
'name' => $item['name']
);
}
$newArray[$currentIndex]['images'][] = $item['image'];
}
echo "<pre>";
var_dump($newArray);
echo "</pre>";

Re-nest arrays into tree using its field as parameter

I'm losing my hair on this one... I have an array structure that print_r's like this (I've hidden unnecessary fields):
[0] => Array
(
[id] => 14
[name] => Foo Directory
)
[1] => Array
(
[id] => 16
[name] => Bar Project
[parent] => Array
(
[id] => 14
[name] => Foo Directory
)
)
[2] => Array
(
[id] => 20
[name] => Baz Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
)
[3] => Array
(
[id] => 10
[name] => Qux Project
[parent] => Array
(
[id] => 16
[name] => Bar Project
)
And I need it to be nested like this:
[0] => Array
(
[id] => 14
[name] => Foo Directory
[children] => Array
(
[id] => 16
[name] => Bar Project
[children] => Array
(
[id] => 20
[name] => Baz Project
)
(
[id] => 10
[name] => Qux Project
)
)
)
What I've tried so far
$projTree = array();
foreach ($projects as $project) {
if (isset($project['parent']))
array_push($projTree['children'], $project);
$projTree['id'] = $project['parent']['id'];
}
But that overwrites the previous inserted element. I also tried to walk it recursively, but couldn't figure out the correct callback for that, since it only operates on the leafs of the tree and I need to fully walk it.
How can I do this? Thanks!
Please try like below:-
<?php
$array = Array(
'0' => Array
(
'id' => 14,
'name' => 'Foo Directory'
),
'1' => Array
(
'id' => 16,
'name' => 'Bar Project',
'parent' => Array
(
'id' => 14,
'name' => 'Foo Directory'
)
),
'2' => Array
(
'id' => 20,
'name' => 'Baz Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
),
'3' => Array
(
'id' => 10,
'name' => 'Qux Project',
'parent' => Array
(
'id' => 16,
'name' => 'Bar Project'
)
)
);
$new_array = array();
$i = 0;
$j = 0;
foreach ($array as $key=>$val){
if(array_key_exists('parent',$val)){
foreach($new_array as $key1=>$val1){
if($val['parent']['name'] === $val1['name']){
$new_array[$key1]['children'][$i]['id'] = $val['id'];
$new_array[$key1]['children'][$i]['name'] = $val['name'];
}else{
foreach ($val1['children'] as $key3=>$val3){
if($val['parent']['name'] === $val3['name']){
$new_array[$key1]['children'][$key3]['children'][$j]['id'] = $val['id'];
$new_array[$key1]['children'][$key3]['children'][$j]['name'] = $val['name'];
}
$j++;
}
$i++;
}
}
}else{
$new_array[$key] = $val;
}
}
echo "<pre/>";print_r($new_array);
Output:- https://eval.in/419591

Codeigniter XML-RPC Response Multi Dimensional Array Approach

I need to create an XML-RPC server that gets cities with their corresponding IDs. What I do as a response is looking weird to me because of unnecessary duplicate entries but I couldnt find a better way.
Array
(
[cityID] => Array
(
[0] => 34
[1] => 35
[2] => 06
)
[cityName] => Array
(
[0] => Istanbul
[1] => Izmir
[2] => Ankara
)
)
I implemented above response. With this implementation:
$response = array(
array(
'cityID' => array(array('34', '35', '06'), 'array'),
'cityName' => array(array('Istanbul', 'Izmir', 'Ankara'), 'array')
),
'struct'
);
The problem is I want to take a response like this :
Array
(
[cities] => Array
(
['34'] => 'Istanbul'
['35'] => 'Izmir'
['06'] => 'Ankara'
)
)
So I tried to implement it like this :
$response = array(
array(
'cities' => array(array('34'=>'Istanbul', '35'=>'Izmir', '06'=>'Ankara'), 'array')
),
'struct'
);
But it fails with this implementation. What am I doing wrong ?
Thanks
You have array like following
$response = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
$newarray = array();
foreach($response['cityID'] as $key => $cityid){
$newarray['cities'][$cityid] = $response['cityName'][$key];
}
print_r($newarray);
You will be getting the expected array.
Array
(
[cities] => Array
(
[34] => Istanbul
[35] => Izmir
[6] => Ankara
)
)
This is how I do it, in Code Igniter 3
$array = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
foreach($array['cityID'] as $key => $cityid){
$response[] = array(array(
$cityid => array($array['cityName'][$key],'string'),
),'struct');
}
return $this->xmlrpc->send_response(array($response,'array'));

Categories