PHP foreach multi array - php

I use the https://github.com/humanmade/Custom-Meta-Boxes/ to make this custom repeatable custom fields to wordpress. The given arrays are like this
Array
(
[0] => Array
(
[photoset-caption] => Array
(
[cmb-field-0] => Caption1
[cmb-field-1] => Caption2
[cmb-field-2] => Caption3
[cmb-field-3] => Caption4
)
[photoset-image] => Array
(
[cmb-field-0] => 17
[cmb-field-1] => 16
[cmb-field-2] => 15
[cmb-field-3] => 14
)
)
[1] => Array
(
[photoset-caption] => Array
(
[cmb-field-0] => Caption1
[cmb-field-1] => Caption2
[cmb-field-2] => Caption3
[cmb-field-3] => Caption4
)
[photoset-image] => Array
(
[cmb-field-0] => 17
[cmb-field-1] => 16
[cmb-field-2] => 15
[cmb-field-3] => 14
)
)
)
The loop it try to make is this.
// get the custom fields for this post
$photoset = get_post_meta($post->ID, 'photoset_group_fields', false );
echo '<div class="photoset">';
echo '<div class="photoset-row">';
foreach($photoset as $photosetloop){
echo '<figure class="photoset-item">';
echo '<div>' . wp_get_attachment_image($photosetloop['photoset-image'], 'large' ) . '</div>';
echo '<figcaption>' . $photosetloop['photoset-caption'] .'</figcaption>';
echo '</figure>';
}
echo '</div>';
echo '</div>';
So the loop haves .photoset-item and inside it have image and caption.
My question how to I foreach it, thanks.
I did update for the array, i have group that I loop.

The simplest way it's to make two foreachs, you can do it recursive with a function if you want as well.
<?php
$photos =array(
'photoset-caption' => array(
'cmb-field-0' => 'Caption1',
'cmb-field-1' => 'Caption2',
'cmb-field-2' => 'Caption3',
'cmb-field-3' => 'Caption4'
),
'photoset-image' => array(
'cmb-field-0' => 17,
'cmb-field-1' => 16,
'cmb-field-2' => 15,
'cmb-field-3' => 14
)
);
foreach($photos as $k => $photo){
echo '<h1>'.$k.'</h1>';
foreach($photo as $key => $value){
echo $key.': '. $value.'<br/>';
}
echo '<hr/>';
}
Check an example here: example

Not the most elegant way to do it, but it does work in this case.
$arr = Array
(
"photoset-caption" => Array
(
"cmb-field-0" => "Caption1",
"cmb-field-1" => "Caption2",
"cmb-field-2" => "Caption3",
"cmb-field-3" => "Caption4"
),
"photoset-image" => Array
(
"cmb-field-0" => 17,
"cmb-field-1" => 16,
"cmb-field-2" => 15,
"cmb-field-3" => 14
)
);
$count = count($arr["photoset-caption"]);
for($i = 0; $i < $count; ++$i) {
echo 'Loop #'.$i.'<br>';
echo $arr["photoset-caption"]["cmb-field-".$i], '<br>';
echo $arr["photoset-image"]["cmb-field-".$i], '<br>';
echo '<br>';
}
You can paste it here:
http://writecodeonline.com/php/

Related

php variables for json reply

this is my reply
Array ([code] => 202 [message] => Accepted [data] => Array ( [resultMap] => Array ( [D3856~H158] => Array ( [AppDay] => * [HosTown] => Colombo 06 [SpecName] => Physiotherapist/Sports Physiotherapist [HosName] => Revival Healthcare Services (Pvt)-Colombo [SpecializationId] => 333 [HosCode] => H158 [AppDate] => Any [DocName] => MR CHAMARA MATHANGAWEERA [DoctorNo] => D3856 ) ) ) [detailMessage] => Success )
now i want to assign variable for this vale and echo in proper way i try this
but it is giving a error msg
Undefined index: DocName
this is my code
////////////////////////////////////
if( $response ){
if ( isset($result->error) )die( $result->error_message );
/* Convert json data to array */
$arr=json_decode( $response, true );
//print_r($arr);
foreach($arr['data'] as $data)
{
$output="Doctor".$data['DocName']."<br/>";
$output="Doctor".$data['SpecName']."<br/>";
$output="Doctor".$data['HosName']."<br/>";
$output="Doctor".$data['Day']."<br/>";
$output="Doctor".$data['Date']."<br/>";
}
Have a look at the structure of your array. The values you are looking for are nested withing several arrays:
$data = array (
'code' => 202,
'message' => 'Accepted',
'data' => array (
'resultMap' => array (
'D3856~H158' => array (
'AppDay' => '*',
'HosTown' => 'Colombo 06',
'SpecName' => 'Physiotherapist/Sports Physiotherapist',
'HosName' => 'Revival Healthcare Services (Pvt)-Colombo',
'SpecializationId' => 333,
'HosCode' => 'H158',
'AppDate' => 'Any',
'DocName' => 'MR CHAMARA MATHANGAWEERA',
'DoctorNo' => 'D3856',
)
)
),
'detailMessage' => 'Success'
);
To print all values for the first record in resultMap:
foreach(current($data['data']['resultMap']) as $key => $value) {
echo $key . " => " . $value . "<br />";
}
You need an additional loop in order to print all records in resultMap.
You can try this working fine
for this fixed array values means below given
foreach($arr['data']['resultMap']['D3856~H158'] as $data)
{
$output="Doctor".$data['DocName']."<br/>";
$output="Doctor".$data['SpecName']."<br/>";
$output="Doctor".$data['HosName']."<br/>";
$output="Doctor".$data['Day']."<br/>";
$output="Doctor".$data['Date']."<br/>";
}
or
The changing array values are given the answer
foreach($arr['data']['resultMap'] as $data)
{
$output="Doctor".$data['DocName']."<br/>";
$output="Doctor".$data['SpecName']."<br/>";
$output="Doctor".$data['HosName']."<br/>";
$output="Doctor".$data['Day']."<br/>";
$output="Doctor".$data['Date']."<br/>";
}

Modify array in loop using php

I have a php multidimensional array(A) and I want to build another array(B) from that array A
My code is
$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)
{
list(,$qid) = explode("_",$k);
$filename .= $question[$k]['name'][$i]."#&";
$insertData[] = array(':quid'=>$qid,':answer'=>$filename);
$i++;
}
echo '<pre>';
print_r($insertData);
echo '</pre>';
It prints
Array
(
[0] => Array
(
[:quid] => 15
[:answer] => aaa#&
)
[1] => Array
(
[:quid] => 16
[:answer] => aaa#&ccc#&
)
)
But I want it to be
Array
(
[0] => Array
(
[:quid] => 15
[:answer] => aaa
)
[1] => Array
(
[:quid] => 16
[:answer] => aaa#&ccc
)
)
$i=0;
foreach($question as $k=>$v)
{
list(,$qid) = explode("_",$k);
$insertData[$i][':quid'] = $qid;
$insertData[$i][':answer'] = implode('#&',$v['name']);
$i++;
}
$filename .= (empty($filename) ? '' : '#&') . $question[$k]['name'][$i];
If aaa#&ccc is a typo and it should be bbb#&ccc, then you can simply do:
foreach($question as $k=>$v)
{
list(,$qid) = explode("_",$k);
$filename = implode("#&", $v['name']);
$insertData[] = array(':quid'=>$qid,':answer'=>$filename);
}
Remove "#&" and place it in condition. It will work;
Just Add a condition.
$filename .= $question[$k]['name'][$i];
if(!empty($filename)){
$filename .= '#&';
}

Php dimensional array

How can I get [jobNo] using loop from array below?
Array
(
[date] => 2014-01-13
[totcomdraft] => 400
[comdraft] => 0
[0] => Array
(
[jobNo] => 1401018618
[dateType] => 1
[comdraft] => 200
)
[1] => Array
(
[jobNo] => 1401018615
[dateType] => 1
[comdraft] => 100
)
[2] => Array
(
[jobNo] => 1401018617
[dateType] => 1
[comdraft] => 100
)
)
Try this
foreach($array as $key=>$val){
if(is_array($val)){
echo $val["jobNo"];
echo "<br />";
}
}
for( $i = 0; $ < count($array); $i++ )
{
print $array[$i]['jobNo'] . "<br>";
}
try with array in-built function :-
$result_array=array_map(function($input_array)
{
return $input_array['desired_column'];
},$input_array_original
);
Use This
foreach($array as $key=>$val){
if(is_array($val)){ // check this value in array
echo $val["jobNo"];
echo "<br />";
}
}
This code should work. I have tested.
$array = array
(
'date' => '2014-01-13',
'totcomdraft' => 400,
'comdraft' => 0,
'0' => array
(
'jobNo' => 1401018618,
'dateType' => 1,
'comdraft' => 200
),
'1' => array
(
'jobNo' => 1401018615,
'dateType' => 1,
'comdraft' => 100
),
'2' => array
(
'jobNo' => 1401018617,
'dateType' => 1,
'comdraft' => 100
)
);
for($i=0; $i<3; $i++){
echo 'Job no:' . $array[$i]['jobNo']."<br>";
}
Output:
Job no:1401018618
Job no:1401018615
Job no:1401018617

PHP, array iteration

i have this array structure:
$ar = [product_info] => Array
(
[pname] => Array
(
[0] => Выделенный сервер DE-22
[1] => Hello 4
[2] => Hello World
)
[pid] => Array
(
[0] => 217
[1] => 342
[2] => 343
)
i want to iterate it like this (in one loop):
foreach ($ar['product_info'] as $item) {
echo $item['pname'];
echo $item['pid']
}
How can i do this? need help
i think a normal for solve it.
$total = count($ar['product_info']['pid']);
for($i=0; $i<$total; $i++){
echo $ar['product_info']['pname'][$i];
echo $ar['product_info']['pid'][$i];
}
<?php
$ar = array(
'product_info' => array(
'pname' => array(
'Product 1',
'Product 2',
'Product 3',
'Product 4 > test <',
),
'pid' => array(
217,
342,
343,
666,
),
),
);
foreach($ar['product_info']['pid'] as $productIndex => $productId)
{
$productName = $ar['product_info']['pname'][$productIndex];
echo '<a href="?product_id=' . intval($productId) . '">';
echo htmlspecialchars($productName);
echo '</a>';
echo '<br />' . PHP_EOL;
}
You need to iterate over the inner arrays, which are int-indexed:
$size=sizeof($ar['product_info']['pname'];
for ($i=0;$i<$size;$i++) {
echo $ar['product_info']['pname'][$i];
echo $ar['product_info']['pid'][$i];
}

How to print specified array value

I am fetch facebook user's working history, and when I print_r, I get an array like this:
Array (
[work] => Array (
[0] => Array (
[employer] => Array (
[id] => 111178415566505
[name] => Liputan 6 SCTV
)
) [1] => Array (
[employer] => Array (
[id] => 107900732566334
[name] => SCTV
)
)
)
[id] => 502163984
)
How do I display only the value from name, so the output will be like this:
Liputan 6 SCTV
SCTV
I used foreach, but always an error always happens.
Try this:
foreach ($array['work'] as $arr) {
echo $arr['employer']['name']."<br>\n";
}
This is assuming your data looks like:
$array = array(
'work' => array(
array(
'employer' => array('id' => 111178415566505, 'name' => 'Liputan 6 SCTV'),
),
array(
'employer' => array('id' => 107900732566334, 'name' => 'SCTV'),
),
),
'id' => 502163984,
);
for instance your array variable name is $test, then you can get name value by
$test['work'][0]['employer']['name']
you can check your array structure using pre tag, like
echo '<pre>';print_r($test);
You can use array_reduce, implode, and closure (PHP 5.3+) to do this.
echo implode("<br/>", array_reduce($array["work"],
function(&$arr, $v){
$arr[] = $v["employer"]["name"];
},array()
));
I assume $arr is working history array. You can use for Then array look like this :
for($i=0, $records = count( $arr['work']); $i < $records; $i++) {
echo $arr['work'][$i]['employer']['name'] ."<br>";
}
Using foreach
foreach( $arr['work'] as $works) {
echo $works['employer']['name'] ."<br>";
}
foreach ($your_array as $your_array_item)
{
echo $your_array_item['work'][0]['employer']['name'] . '<br>';
}
$count=count($array);
for($i=0,$i<=$count;$i++){
echo $array['work'][$i]['employer']['name'];
}
This will work.. Dynamically..
foreach ($array['work'] as $val_arr) {
echo $val_arr['employer']['name']."<br />";
}

Categories