My array:
array(1) {
["farm"] => array(1) {
["animals"] => array(1) {
[horses] => array(4) {
["fred"] => string(4) "fred"
["sam"] => string(4) "sam"
["alan"] => string(4) "alan"
["john"] => string(4) "john"
}
}
}
}
My function (created by #FrayneKonok)
$id = "2";
$search = "horses";
get_values($arr);
function get_values($arr, $id) {
global $search;
foreach($arr as $key => $value) {
if($key == $search) {
if(is_array($value)) {
echo(join("<a href='mypage.php?id=".$id."&dir=".$value."'><li>",array_keys($value)));
echo("</li></a>");
} else {
echo($value);
}
} else {
get_values($value);
}
}
}
get_values($array,$id);
The result is:
"fred"
<li>sam</li>
<li>alan</li>
<li>john</li>
The result I'm looking for:
<li>fred</li>
<li>sam</li>
<li>alan</li>
<li>john</li>
Another example is when i use if($search = "farm") my result becomes:
"animals"
When the result I'm looking for is:
<li>animals</li>
Online link
Array and input, function call
$arr = array("farm" =>
array("animals"=>
array("horses" =>
array("fred" => "fred",
"sam" => "sam",
"alan" => "alan",
"john" => "john")
)
)
);
$search = 'farm';
get_values($arr);
Function:
function get_values($arr){
global $search;
foreach($arr as $key => $value){
if($key == $search){
if(is_array($value)){
$keys = array_keys($value);
if(count($keys) > 1){
for($i = 0; $i < count($keys); $i++){
echo '<li>'.$keys[$i].'</li>';
}
}else{
echo '<li>'.$keys[0].'</li>';
}
}
else{
echo $value;
}
}else{
get_values($value);
}
}
}
Output
<li>animals</li>
Also tested for the horses.
I suggest using another foreach
Instead of
if(is_array($value)){
echo join("<a href='mypage.php?id=".$id."&dir=".$value."'><li>",array_keys($value));
echo "</li></a>";
}
Use
if(is_array($value)){
foreach( $value as $k => $v ) {
echo "<a href='mypage.php?id=".$id."&dir=".$k."'><li>".$k."</li></a>\n";
}
}
Also don't forget to pass $id each time
The whole thing becomes:
$arr = array( "farm"=> array( "animals"=> array( "horses"=> array( "fred" => "fred", "sam" => "sam", "alan" => "alan", "john" => "john" ) ) ) );
$id = "2";
$search = "horses";
get_values($arr, $id); // <-- pass $id here
function get_values($arr, $id){
global $search;
foreach($arr as $key => $value){
if($key == $search){
if(is_array($value)){
foreach( $value as $k => $v ) {
echo "<a href='mypage.php?id=".$id."&dir=".$k."'><li>".$k."</li></a>\n";
}
}
else{
echo $value;
}
}else{
get_values($value, $id ); // <-- pass $id here to
}
}
}
Related
Sorry I don't know how to provide a more appropriate subject
How can I get only the remaining arrays (in this case: banana) in the $regular_items array (which doesn't fall into the first 2 conditions)?
I have tried various if conditions for days still doesn't work.
The following codes give me all 3 $cart array in the $regular_items array.
<?php
$offers[0]['buy_type'] = 'brand';
$offers[0]['buy_type_id'] = '1';
$offers[0]['name'] = 'brand1';
$offers[1]['buy_type'] = 'item';
$offers[1]['buy_type_id'] = '2';
$offers[1]['name'] = 'item2';
$carts[2]['name'] = 'orange';
$carts[2]['brand_id'] = '3';
$carts[2]['item_id'] = '2';
$carts[4]['name'] = 'banana';
$carts[4]['brand_id'] = '6';
$carts[4]['item_id'] = '4';
$carts[6]['name'] = 'apple';
$carts[6]['brand_id'] = '1';
$carts[6]['item_id'] = '6';
foreach ($carts as $cart=>$c) {
foreach ($offers as $offer=>$o) {
if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
// if this condition is true, add to $promo_brands array
$promo_brands[$o['buy_type_id']][$cart] = $c;
} else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
// otherwise if this condition is true, add to $promo_items array
$promo_items[$cart] = $c;
} else {
// if none of the above, add to $regular_items
$regular_items[$cart] = $c;
}
}
}
echo 'Promo Brands<br />';
var_dump($promo_brands);
echo '<br />Promo Items<br />';
var_dump($promo_items);
echo '<br />Regular Items<br />';
var_dump($regular_items);
?>
Desired OUTPUT
Promo Brands
array(1) { [1]=> array(1) { [6]=> array(3) { ["name"]=> string(5) "apple" ["brand_id"]=> string(1) "1" ["item_id"]=> string(1) "6" } } }
Promo Items
array(1) { [2]=> array(3) { ["name"]=> string(6) "orange" ["brand_id"]=> string(1) "3" ["item_id"]=> string(1) "2" } }
Regular Items
array(1) { [4]=> array(3) { ["name"]=> string(6) "banana" ["brand_id"]=> string(1) "6" ["item_id"]=> string(1) "4" } }
SOLVED:
unsetting the array elements once it meets the condition before going to another condition(s)
$tmp_carts = $carts;
foreach ($tmp_carts as $cart=>$c) {
foreach ($offers as $offer=>$o) {
if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
// if this condition is true, add to $promo_brands array
$promo_brands[$o['buy_type_id']][$cart] = $c;
unset($tmp_carts[$cart]);
} else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
// otherwise if this condition is true, add to $promo_items array
$promo_items[$cart] = $c;
unset($tmp_carts[$cart]);
} else {
}
}
}
$regular_items = $tmp_carts;
Just you need to check
Use if condition.
if($c['name'] == "banana")
{
$regular_items[$cart] = $c;
}
OR
Use in_array().
if(in_array("banana", $c))
{
$regular_items[$cart] = $c;
}
Code
$offers[1]['buy_type'] = 'item';
$offers[1]['buy_type_id'] = '2';
$offers[1]['name'] = 'item2';
$carts[2]['name'] = 'orange';
$carts[2]['brand_id'] = '3';
$carts[2]['item_id'] = '2';
$carts[4]['name'] = 'banana';
$carts[4]['brand_id'] = '6';
$carts[4]['item_id'] = '4';
$carts[6]['name'] = 'apple';
$carts[6]['brand_id'] = '1';
$carts[6]['item_id'] = '6';
foreach ($carts as $cart=>$c) {
foreach ($offers as $offer=>$o) {
if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
$promo_brands[$o['buy_type_id']][$cart] = $c;
} else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
$promo_items[$cart] = $c;
} else {
if($c['name'] == "banana")
{
$regular_items[$cart] = $c;
}
}
}
}
echo '<br />Regular Items<br />';
print_r($regular_items);
?>
Output
Regular Items
Array
(
[4] => Array
(
[name] => banana
[brand_id] => 6
[item_id] => 4
)
)
Use unset() to remove array elements which already comes in condition.
foreach ($carts as $cart=>$c) {
foreach ($offers as $offer=>$o) {
if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
$promo_brands[$o['buy_type_id']][$cart] = $c;
unset($carts[$cart]);
} else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
$promo_items[$cart] = $c;
unset($carts[$cart]);
}
}
}
echo "<pre>";
print_r($carts);
Output
Array
(
[4] => Array
(
[name] => banana
[brand_id] => 6
[item_id] => 4
)
)
are you wanting to retrieve the banana always or do you need to retrieve it by name? try the in_array function
foreach ($carts as $cart=>$c) {
if(in_array("banana", $c)){
echo "true";
}else{
echo "nope";
}
You can try following trick, using in_array function.
$offers[0]['buy_type'] = 'brand';
$offers[0]['buy_type_id'] = '1';
$offers[0]['name'] = 'brand1';
$offers[1]['buy_type'] = 'item';
$offers[1]['buy_type_id'] = '2';
$offers[1]['name'] = 'item2';
$carts[2]['name'] = 'orange';
$carts[2]['brand_id'] = '3';
$carts[2]['item_id'] = '2';
$carts[4]['name'] = 'banana';
$carts[4]['brand_id'] = '6';
$carts[4]['item_id'] = '4';
$carts[6]['name'] = 'apple';
$carts[6]['brand_id'] = '1';
$carts[6]['item_id'] = '6';
$promo_brands = array();
$promo_items = array();
$regular_items = array();
$temp_regular_items = array();
foreach ($carts as $cart=>$c) {
foreach ($offers as $offer=>$o) {
if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
$promo_brands[$cart] = $c;
} else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
$promo_items[$cart] = $c;
} else {
$regular_items[$cart] = $c;
}
}
}
foreach($regular_items as $regitem => $r)
{
if(!in_array($r, $promo_brands) && !in_array($r, $promo_items))
$temp_regular_items[$regitem] = $r;
}
echo 'Promo Brands<br />';
echo "<pre>"; print_r($promo_brands); echo "</pre>";
echo '<br />Promo Items<br />';
echo "<pre>"; print_r($promo_items); echo "</pre>";
echo '<br />Regular Items<br />';
echo "<pre>"; print_r($temp_regular_items); echo "</pre>";
This gives output as follows:
Promo Brands
Array
(
[6] => Array
(
[name] => apple
[brand_id] => 1
[item_id] => 6
)
)
Promo Items
Array
(
[2] => Array
(
[name] => orange
[brand_id] => 3
[item_id] => 2
)
)
Regular Items
Array
(
[4] => Array
(
[name] => banana
[brand_id] => 6
[item_id] => 4
)
)
Hope this will help you..!
Searched for so long but didn't get any feasible answer.
A) Input:
$array = array(
'order_source' => array('google','facebook'),
'order_medium' => 'google-text'
);
Which looks like:
Array
(
[order_source] => Array
(
[0] => google
[1] => facebook
)
[order_medium] => google-text
)
B) Required output:
order_source=google&order_source=facebook&order_medium=google-text
C) What I've tried (http://3v4l.org/b3OYo):
$arr = array('order_source' => array('google','facebook'), 'order_medium' => 'google-text');
function bqs($array, $qs='')
{
foreach($array as $par => $val)
{
if(is_array($val))
{
bqs($val, $qs);
}
else
{
$qs .= $par.'='.$val.'&';
}
}
return $qs;
}
echo $qss = bqs($arr);
D) What I'm getting:
order_medium=google-text&
Note: It should also work for any single dimensional array like http_build_query() works.
I hope that this is what you are looking for, it works with single to n-dimensinal arrays.
$walk = function( $item, $key, $parent_key = '' ) use ( &$output, &$walk ) {
is_array( $item )
? array_walk( $item, $walk, $key )
: $output[] = http_build_query( array( $parent_key ?: $key => $item ) );
};
array_walk( $array, $walk );
echo implode( '&', $output ); // order_source=google&order_source=facebook&order_medium=google-text
You don't really need to do anything special here.
$array = array(
'order_source' => array('google', 'facebook'),
'order_medium' => 'google-text'
);
$qss = http_build_query($array);
On the other side:
var_dump($_GET);
Result:
array(2) {
["order_source"]=>
array(2) {
[0]=>
string(6) "google"
[1]=>
string(8) "facebook"
}
["order_medium"]=>
string(11) "google-text"
}
This really is the best way to send arrays as GET variables.
If you absolutely must have the output as you've defined, this will do it:
function bqs($array, $qs = false) {
$parts = array();
if ($qs) {
$parts[] = $qs;
}
foreach ($array as $key => $value) {
if (is_array($value)) {
foreach ($value as $value2) {
$parts[] = http_build_query(array($key => $value2));
}
} else {
$parts[] = http_build_query(array($key => $value));
}
}
return join('&', $parts);
}
Although as you found in the comments if you are trying to pass this as $_GET you will have override problems, the solution to your problem to get desired results using recursive functions would be:
function bqs($array, $qs='',$index = false)
{
foreach($array as $par => $val)
{
if($index)
$par = $index;
if(is_array($val))
{
$qs = bqs($val, $qs,$par);
}
else
{
$qs .= $par.'='.$val.'&';
}
}
return $qs;
}
where i am concatinating the $qs string if it's an array and passing the index as a reference along with the value if it's an array()
fixed
After supplying the $index you do not need to concatenate again. See here: http://3v4l.org/QHF5G
Can anyone help output a multidimensional array, please.
Not to sure where I have gone wrong.
The sort ordering looks correct, but its not displaying the results.
<?php
$atest = Array ( "0" => Array ( "id" => "913", "testname" => "qwerty1", "i" => "1" ),
"1" => Array ( "id" => "913", "testname" => "test22", "i" => "2" ),
"2" => Array ( "id" => "913", "testname" => "American1", "i" => "3" ),
"3" => Array ( "id" => "913", "testname" => "Eagle4", "i" => "4" ) );
$range = range('A','Z');
$output = array();
$output['#'] = array();
foreach($range as $letter){
$output[$letter] = array();
}
foreach($atest as $test){
if ($test["testname"] !='') {
$uc = ucfirst($test["testname"]);
if(array_search($uc[0], $range) === FALSE){
$output['#'][] = $uc;
} else {
$output[$uc[0]][] = $uc;
}
}
}
foreach($output AS $letter => $result){
echo $letter . "<br/>--------<br/>\n";
sort($result);
foreach($result AS $indresult){
echo '' . $indresult['testname'] . '<br/>';
}
echo "<br/>\n";
}
?>
You're not putting the whole sub-array into $output, you're only putting $uc. Change the middle foreach loop to:
foreach($atest as $test){
if ($test["testname"] !='') {
$uc = ucfirst($test["testname"]);
if(array_search($uc[0], $range) === FALSE){
$output['#'][] = $test;
} else {
$output[$uc[0]][] = $test;
}
}
}
Try to use print_r function, for example for array dump:
print_r($atest);
Try this
$output[$uc[0]][] = $test;
instead of
$output[$uc[0]][] = $uc; // only the name is stored.
See demo here
I have this set of data that I get from html form. It is basically a multidimensional array.
Data
array(3) {
["r1"]=>
array(2) {
[0]=>
string(1) "2"
[1]=>
string(1) "4"
}
["r2"]=>
array(2) {
[0]=>
string(1) "5"
[1]=>
string(2) "96"
}
["tekma_id"]=>
array(2) {
[0]=>
string(1) "7"
[1]=>
string(1) "8"
}
}
Problem: What i want to do, is to go over this array and for each iteration create a data variable(array).
So for example:
First iteration:
$data = array(
'r1' => '2'
'r2' => '5'
'tekma_id' => '7'
)
Second iteration:
$data = array(
'r1' => '4'
'r2' => '96'
'tekma_id' => '8'
)
I've tried with this:
foreach ($data as $key => $value) {
foreach ($value as $index => $v) {
echo "<br>";
echo "r1: $v";
echo "<br>";
echo "r2: $v";
echo "<br>";
echo "tekma_id: $v";
}
}
But it didn't work. Sorry for my bad english and thanks for any help.
Cheers!
How about this?
$array = array(
'r1' => array(2, 4),
'r2' => array(5, 96),
'tekma_id' => array(7, 8));
$keys = array_keys($data);
$iterations = count($array[$keys[0]]);
for($i = 0; $i < $iterations; $i++) {
$data = array();
foreach($array as $key => $value) {
$data[$key] = $value[$i];
}
print_r($data);
}
Output:
Array
(
[r1] => 2
[r2] => 5
[tekma_id] => 7
)
Array
(
[r1] => 4
[r2] => 96
[tekma_id] => 8
)
Try this:
$keys = array_keys($data);
$count = count(array_shift(array_values($data)));
for ($i = 0; $i<$count; $i++) {
$result = array();
foreach ($keys as $key) {
$result[$key] = $data[$key][$i];
}
var_dump($result);
}
I have some case like this:
I have json data:
[{
"1377412272": {
"user_id": "1374050643",
"date": "2013-08-24",
"ip": "::1"
}
},
{
"1377412279": {
"user_id": "1374050643",
"date": "2013-08-25",
"ip": "::1"
}
}
, {
"1377412287": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
}, {
"1377413058": {
"user_id": "1374050643",
"date": "2013-08-25",
"ip": "::1"
}
},
{
"1377413069": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
}
, {
"1377413074": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
},
{
"1377413079": {
"user_id": "1377346094",
"date": "2013-08-25",
"ip": "::1"
}
}
]
An then, I have convert to array PHP
$newArr = array();
foreach ($view['con'] as $key => $value) {
foreach ($value as $k => $v) {
if (isset($newArr[$v['user_id']][$v['date']])) {
$newArr[$v['user_id']][$v['date']]++;
}
else
$newArr[$v['user_id']][$v['date']] = 1;
$newArr[$v['user_id']][$v['date']] = isset($newArr[$v['user_id']][$v['date']]) ? $newArr[$v['user_id']][$v['date']]++ : 1;
}
}
Script above have result in json_encode with structure like this:
Array
(
[A] => Array
(
[2013-08-24] => 1
[2013-08-25] => 2
)
[B] => Array
(
[2013-08-25] => 4
)
)
and finally, I want it to be javascript object
[
["date","A","B"],
[2013-08-24,1,0],
[2013-08-25,2,4]
]
How to make it?...
To get output like this yo should do
$countArr = array();
foreach ($data as $key => $value)
{
foreach ($value as $k => $v)
{
if (isset($countArr[$v['date']][$v['user_id']]))
{
$countArr[$v['date']][$v['user_id']]++;
}
else
{
$countArr[$v['date']][$v['user_id']] = 1;
}
}
}
$newArr = array();
foreach ($countArr as $date => $val)
{
$row = array($date);
$newArr[] = array_merge(array($date), array_values($val));
}
echo "<pre>";
print_r($newArr);
echo json_encode($newArr)
If you print out $newArr it will look like this
Array
(
[0] => Array
(
[0] => 2013-08-24
[1] => 1
)
[1] => Array
(
[0] => 2013-08-25
[1] => 2
[2] => 4
)
)
json_encode will output
[["2013-08-24",1],["2013-08-25",2,4]]
I'm afraid you need to code everything manually.
One (not too simple) solution is this:
<?php
$ori_list = array(
'A'=> array(
'2013-08-24' => 1,
'2013-08-25' => 2,
),
'B'=> array(
'2013-08-24' => 3,
),
);
$modif_list = array();
// prepare the header
$header = array('date');
foreach($ori_list as $key=>$val){
if(!array_key_exists($key, $header)){
$header[] = $key;
}
}
$modif_list[] = $header;
// prepare the date_list
$date_list = array();
foreach($ori_list as $key=>$val){
foreach($val as $date=>$num){
// add the initial row for every date
$registered = false;
foreach($date_list as $date_row){
if($date_row[0] == $date){
$registered = true;
break;
}
}
if(!$registered){
$date_row = array($date);
for($i=0; $i<count($header)-1; $i++){
$date_row[] = 0;
}
$date_list[] = $date_row;
}
// put the right value to the right row
$first_index = 0;
$second_index = 0;
for($i=1; $i<count($header); $i++){
if($header[$i] == $key){
$second_index = $i;
break;
}
}
for($i=0; $i<count($date_list); $i++){
if($date == $date_list[$i][0]){
$first_index = $i;
break;
}
}
$date_list[$first_index][$second_index] = $num;
}
}
$modif_list[] = $date_list;
echo 'The PHP';
echo '<pre>';
var_dump($modif_list);
echo '</pre>';
echo 'The JSON';
echo '<pre>';
echo json_encode($modif_list);
echo '</pre>';
?>
The code will produce something like this (which I hope is what you want):
The PHP
array(2) {
[0]=>
array(3) {
[0]=>
string(4) "date"
[1]=>
string(1) "A"
[2]=>
string(1) "B"
}
[1]=>
array(2) {
[0]=>
array(3) {
[0]=>
string(10) "2013-08-24"
[1]=>
int(1)
[2]=>
int(3)
}
[1]=>
array(3) {
[0]=>
string(10) "2013-08-25"
[1]=>
int(2)
[2]=>
int(0)
}
}
}
The JSON
[["date","A","B"],[["2013-08-24",1,3],["2013-08-25",2,0]]]
PHP
$arr = array("id"=>"1");
json_encode($arr);
Javascript + PHP
var json = jQuery.parseJSON(<?=$arr?>);
var id = json.id;
I think this is what you want
$newArray = array
(
"A" => array
(
"2013-08-24" => 1,
"2013-08-25" => 2
),
"B" => array
(
"2013-08-25" => 4
)
);
$uids=array();
$da = array();
foreach($na as $uid => $value)
{
$uids[] = $uid;
foreach($value as $date => $count)
{
$da[$date][$uid]=$count;
}
}
$ra = array(array("date"));
foreach($uids as $uid)
{
$ra[0][] = $uid;
}
$i = 1;
foreach($da as $date => $value)
{
$ra[$i][] = $date;
foreach($uids as $uid)
{
if(array_key_exists($uid,$value))
{
$ra[$i][] = $value[$uid];
}
else
{
$ra[$i][] = 0;
}
}
$i++;
}
print(json_encode($ra));
Output:
[["date","A","B"],["2013-08-24",1,0],["2013-08-25",2,4]]
In php, lets call it array.php:
// Your final statment of your array.php script must be an echo statment to send the array to jQuery
$ar = array('item1' => 'value1');
$ret_val['a'] = $ar;
echo json_encode($ret_val);
In jQuery (for example, in the callback of $.post, but you can use $.ajax as well)
$.post("/path/to/array.php,
null, // not passing any values to array.php
function(data) {
console.log (data.a.item1); // writes 'value1' to the console
// (note that the 'a' in data.a.item1 in jQuery is the same as the 'a' in $ret_val in PHP)
},
"json");
}
Then you deal with the return value anyway you like, including creating the final object you seek.