Php dimensional array - php

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

Related

From php multidimensional array trying to create nested ul li menu (unlimited nested levels)

Here is what i have got http://codepad.org/iDoXXsLX
Have array like this
Array
(
[0] => Array
(
[NumberRenamed] => 17
[TopicName] => Products
[UpperLevelNumberRenamed] => 0
)
[17] => Array
(
[0] => Array
(
[1] => Array
(
[NumberRenamed] => 18
[TopicName] => Computers
[UpperLevelNumberRenamed] => 17
)
)
)
[18] => Array
(
[0] => Array
(
[2] => Array
(
[NumberRenamed] => 16
[TopicName] => Laptops
[UpperLevelNumberRenamed] => 18
)
)
)
[16] => Array
(
[0] => Array
(
[4] => Array
(
[NumberRenamed] => 8
[TopicName] => Dell
[UpperLevelNumberRenamed] => 16
)
)
)
)
Top level item is Products, first sub-level item is Computers, next sub-level is Laptops, then again next sub-level Dell
For each sub-level item UpperLevelNumberRenamed == to closest upper level NumberRenamed.
Want to get result like this
Products
Computers
Laptops
Dell
Acer
Desktops
Home
Tried this
foreach( $main_topics as $k_main_topics => $v_main_topics ){
if( isset($v_main_topics['UpperLevelNumberRenamed']) and $v_main_topics['UpperLevelNumberRenamed'] == 0 ){
//print only top level topics
echo $v_main_topics['TopicName']. '<br/>';
}
else{//if not top level topic
foreach( $v_main_topics[0] as $k_v_main_topics_0 => $v_v_main_topics_0 ){
echo $v_v_main_topics_0['TopicName']. '<br/>';
}//foreach( $v_main_topics[0] as $k_v_main_topics_0 => $v_v_main_topics_0 )
}//else{
}//foreach( $main_topics as $k_main_topics => $v_main_topics )
But get this
Products
Home
Computers
Laptops
Desktops
Dell
Acer
Something incorrect, but can not understand what. Please, advice what need to correct/change in the code
Trying another way
Initial array is one dimensional array. Trying to get ul li navigation from one dimensional.
Here is what i did http://codepad.org/OLtxyL4X
Here's a summary of what it does:
flatten the array recursively
build a multi-dimensional relation map
create 1D relationships that link UpperLevelNumberRenamed to NumberRenamed
print out the multi-dimensional as an ul-li list.
Here it is:
$flat = array();
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($main_topics)) as $i)
$flat[] = $i;
$final = array();
$defs = array();
for ($i = 0; $i < count($flat); $i += 3)
if ($flat[$i + 2] == 0) {
$final[$flat[$i + 1]] = array();
$defs[$flat[$i]] = &$final[$flat[$i + 1]];
} else {
$defs[$flat[$i + 2]][$flat[$i + 1]] = array();
$defs[$flat[$i]] = &$defs[$flat[$i + 2]][$flat[$i + 1]];
}
function array2ul($array) {
$out = "<ul>";
foreach($array as $key => $elem)
$out = is_array($elem) ?
$out . "<li><span>$key</span>" . array2ul($elem) . "</li>" :
$out = $out."<li><span>$key:[$elem]</span></li>";
$out = $out . "</ul>";
return $out;
}
echo array2ul($final);
Output:
<ul><li><span>Products</span><ul><li><span>Computers</span><ul><li><span>Laptops</span><ul><li><span>Dell</span><ul></ul></li><li><span>Acer</span><ul></ul></li></ul></li><li><span>Desktops</span><ul></ul></li></ul></li></ul></li><li><span>Home</span><ul></ul></li></ul>
This shall be a working example using recursion, not tested though:
Define the array
$main_array = Array
(
'10' => Array
(
'name' => 'Products'
'children' => Array
(
'12' => Array
(
'name' => 'Laptop',
'children' => Array
(
'13' => Array
(
'name' => 'Dell',
),
'14' => Array
(
'name' => 'Acer',
)
)
)
'14' => Array
(
'name' => 'Desktop',
'children' => Array
(
'15' => Array
(
'name' => 'Sony',
),
'16' => Array
(
'name' => 'Apple',
)
)
),
)
)
)
Create and call the function :
function createList($main_topics)
{
if($main_topics == null || sizeof($main_topics) <= 0)
{
return '';
}
$list = '<ul>';
foreach($main_topics as $k_main_topics => $v_main_topics )
{
$list .= '<li id="' . $k_main_topics'"> '. $v_main_topics['name'] . ' ' . createList(isset($v_main_topics["children"]) ? $v_main_topics["children"] : null) . '</li>' ;
}
$list .= '</ul>';
return $list;
}
echo createList($main_array);

Transform a multidimensional array in php

This loop
$demo = array();
for($i=0;$i<count($big_array);$i++){
echo 'Page['.$i.'][0]: '.$big_array[$i][0].'<br>';
for($j=1;$j<count($big_array[$i]);$j++){
echo 'Email['.$i.']['.$j.']: '.$big_array[$i][$j].'<br>';
$demo[$big_array[$i][$j]][] = $big_array[$i][$j-1]; //something is not ok with this
}
}
gives me this:
Page[0][0]: http://www.example.com/impressum
Email[0][1]: sales#example.com
Email[0][2]: support#example.com
Page[1][0]: http://www.example.com/termsofuse
Email[1][1]: support#example.com
Email[1][2]: terms1#example.com
Email[1][3]: terms2#example.com
Email[1][4]: ad2#example.com
Page[2][0]: http://www.example.com/adpolicy
Email[2][1]: support#example.com
Email[2][2]: ad1#example.com
Email[2][3]: ad2#example.com
Email[2][4]: ad1#example.com
How can I transform it to get this result:
sales#example.com
http://www.example.com/impressum
support#example.com
http://www.example.com/impressum
http://www.example.com/termsofuse
http://www.example.com/adpolicy
terms1#example.com
http://www.example.com/termsofuse
terms2#example.com
http://www.example.com/termsofuse
ad2#example.com
http://www.example.com/termsofuse
http://www.example.com/adpolicy
ad1#example.com
http://www.example.com/adpolicy
var_dump($big_array):
array ( 0 => array ( 0 => 'http://www.example.com/impressum', 1 => 'sales#example.com', 2 => 'support#example.com', ), 1 => array ( 0 => 'http://www.example.com/termsofuse', 1 => 'support#example.com', 2 => 'terms1#example.com', 3 => 'terms2#example.com', 4 => 'ad2#example.com', ), 2 => array ( 0 => 'http://www.example.com/adpolicy', 1 => 'support#example.com', 2 => 'ad1#example.com', 3 => 'ad2#example.com', 4 => 'ad1#example.com', ), )
$array = array ( 0 => array ( 0 => 'http://www.example.com/impressum', 1 => 'sales#example.com', 2 => 'support#example.com', ), 1 => array ( 0 => 'http://www.example.com/termsofuse', 1 => 'support#example.com', 2 => 'terms1#example.com', 3 => 'terms2#example.com', 4 => 'ad2#example.com', ), 2 => array ( 0 => 'http://www.example.com/adpolicy', 1 => 'support#example.com', 2 => 'ad1#example.com', 3 => 'ad2#example.com', 4 => 'ad1#example.com', ), );
print_r($array);
$final = array();
foreach ( $array as $group )
{
for ( $i=1; $i<count($group); $i++ )
{
$final[$group[$i]][] = $group[0];
}
}
print_r($final);
Here is the PHP Playground result.
To format it like your example:
foreach ( $final as $email => $links )
{
echo $email . "\n";
foreach ( $links as $link )
{
echo " " . $link . "\n";
}
}

PHP foreach multi array

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/

array grouping which contains same fields

I have an array which contains following values.
array(
'dates' => array(
(int) 0 => '2013-04-22',
(int) 1 => '2013-04-23',
),
'publisherName' => array(
(int) 0 => 'Comp1',
(int) 1 => 'Comp2',
),
'loaded' => array(
(int) 0 => (int) 2189,
(int) 1 => (int) 37,
),
'clicks' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
),
'ctr' => array(
(int) 0 => (int) 0,
(int) 1 => (int) 0,
)
)
What I want to produce is getting company based data on different dates like the array below.
How am I able to create an array which is like;
array (
'2013-04-22'=>array(
'publisherName'=>'Comp1',
'loaded'=>2189,
'clicks'=>0,
'ctr'=>0),
'2013-04-23'=>array(
'publisherName'=>'Comp2',
'loaded'=>37,
'clicks'=>0,
'ctr'=>0)
...
)
Which contains daily stats but which comes from publishername field.
Any ideas?
Here's an example that doesn't rely on any keys, the only requirement is that date is the first array in your original array:
// $array is your original array
$dates = array_shift($array);
$output = array();
foreach ($array as $k => $a) {
foreach ($a as $i => $v) {
$output[$i][$k] = $v;
}
}
$output = array_combine($dates, $output);
Let the initial array be $a and the desired array $b;
Code:
$b = array();
$count = 0;
foreach($a['dates'] as $date) {
$b[$date] = array(
'name' => $a['publisherName'][$count],
'loaded'=> $a['loaded'][$count],
'clicks'=> $a['clicks'][$count],
'ctr'=> $a['ctr'][$count]
);
$count++;
}
Result:
Array
(
[2013-04-22] => Array
(
[name] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[name] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)
<?php
$data = $yourarray;
$new = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($new);
?>
$newArr = array();
foreach($data['dates'] as $key=>$value) {
$new[$value] = array('name'=>$data['publisherName'][$key],'loaded'=>$data['loaded'][$key],'clicks'=>$data['clicks'][$key],'ctr'=>$data['ctr'][$key]);
}
echo '<pre>';
print_r($newArr);
$new_arr = your array;
$finalArray = array();
foreach($new_arr['dates'] as $key => $value){
$finalArray[$value] = array(
'publisherName'=>$new_arr['publisherName'][$key],
'loaded'=>$new_arr['loaded'][$key],
'clicks'=>$new_arr['clicks'][$key],
'ctr'=>$new_arr['ctr'][$key]
);
}
Output :
Array
(
[2013-04-22] => Array
(
[publisherName] => Comp1
[loaded] => 2189
[clicks] => 0
[ctr] => 0
)
[2013-04-23] => Array
(
[publisherName] => Comp2
[loaded] => 37
[clicks] => 0
[ctr] => 0
)
)

merge two keys on different identical keys in a multidimensional array

I have a single multidimensional array that I'm trying to merge keys. I'd like to merge where identical keys occur on a separate key from the one I'm trying to merge.
My current array looks like so.
Array
(
[0] => Array
(
[zone_id] => 2
[zone_title] => Users
[link_title] => Users
)
[1] => Array
(
[zone_id] => 2
[zone_title] => Users
[link_title] => Add User
)
[2] => Array
(
[zone_id] => 3
[zone_title] => Locations
[link_title] =>
)
)
I would like to leave the array as is with the exception of merging together arrays that have identical zone_id keys.
Result
Array
(
[0] => Array
(
[zone_id] => 2
[zone_title] => Users
[link_title] => Array
(
[0] => Users
[1] => Add user
)
)
[1] => Array
(
[zone_id] => 3
[zone_title] => Locations
[link_title] =>
)
)
<?php
function merge_keys($arr){
for ($key = 0; $key < count($arr); $key++) {
$zone_id = $arr[$key]['zone_id'];
$index = search($arr, $zone_id);
if ($index != $key && $index != -1){
$link_title = $arr[$key]['link_title'];
$link_title2 = $arr[$index]['link_title'];
$arr[$key]['zone_id'] = $zone_id;
$arr[$key]['zone_title'] = $arr[$key]['zone_title'];
$arr[$key]['link_title'] = array($link_title, $link_title2);
unset($arr[$index]);
}
}
return $arr;
}
function search($arr, $zone_id){
for ($i = count($arr) - 1; $i >= 0 ; $i--) {
$item = $arr[$i];
$tmp_zone_id = $item['zone_id'];
if($tmp_zone_id == $zone_id){
return $i;
}
}
return -1;
}
$arr = array(array('zone_id' => 2, 'zone_title' => 'Users', 'link_title' => 'Users'),
array('zone_id' => 2, 'zone_title' => 'Users', 'link_title' => 'Add User'),
array('zone_id' => 3, 'zone_title' => 'Locations', 'link_title' => ''));
echo "Before change: \n";
print_r($arr);
$arr = merge_keys($arr);
echo "After change: \n\n";
print_r($arr);
?>

Categories