PHP dealing with arrays - php

Array
(
[0] => Array
(
[uid] => 43543534
)
)
I'm trying to get output as [0] => [43543534]
I tried foreach() but I'm getting string as output
Update How do i find max value now in this?

Why don't you have only 1 dimensional array array('0' => 43543534), if you have only 'uid' in the second one
foreach ($yourArray as $key => $val) {
echo '['.$key.'] => ['.$val['uid'].']<br />';
}

$var = array( '0' => array ( 'uid' => '43543534' ) );
foreach($var as $arr):
echo $arr['uid'];
endforeach;

Your question is very unclear, but here are two ways to accomplish that using the original array:
$array = array( '0' => array ( 'uid' => '43543534' ) );
$result[0] = $array[0]['uid];
or with foreach
$array = array( '0' => array ( 'uid' => '43543534' ) );
foreach($array as $a){
$result[] = $a['uid'];
}

Related

How can I get a key value from two arrays on match?

I have 2 arrays, I'm trying to find any matches and return 'url from $array_full.
I tried array_intersect($array_full, $array_ids), but it doesn't work.
$array_full = array
(
Array
(
'#attributes' => Array
(
'topicid' => 102000,
'url' => 'Velkommen.htm',
'alias' => 'Velkommen'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130313,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130315,
'url' => 'SPedestal/Applikationer/LoadSharing/Indstillinger.htm',
'alias' => 'LOS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130312,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
)
);
$array_ids = array('130312', '130315');
I expect to get an array of matched url's, like:
array('WStation/WAS_Indstillinger.htm','SPedestal/Applikationer/LoadSharing/Indstillinger.htm')
A simple couple of foreach loops seems the easiest approach
$results = [];
foreach ( $array_full as $a ) {
foreach ( $a as $item ) {
if ( in_array($item['topicid'], $array_ids) ) {
$results[] = $item['url'];
}
}
}
print_r($results);
RESULT
Array
(
[0] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[1] => WStation/WAS_Indstillinger.htm
)
You will have to make foreach inside foreach to find item that is matching to ID.
Something like this (not tested, may contain some typos).
foreach($array_ids as $id) {
foreach($array_full as $key => $fullItem) {
if($fillItem['#attributes']['topicid'] != $id) {
continue;
}
//do what you need with $fullItem array
$key; // this is the key you want
}
}
you can use array_map, in_array to get the URL's
$result = [];
array_map(function($v) use ($array_ids,&$result){
$result[] = in_array($v['#attributes']['topicid'], $array_ids) ? $v['#attributes']['url'] : '';
}, $array_full);
Result:-
echo '<pre>';
print_r(array_filter($result));
Array
(
[2] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[3] => WStation/WAS_Indstillinger.htm
)

Getting a value from an associative array.

I'm a beginner and for some reason I'm having trouble with this one. I have the following associative array.
Array
(
[0] => Array
(
[name_type] => UnixName
[name] => charles
)
[1] => Array
(
[name_type] => DNSFQDN
[name] => charles.mydnsdomain.com
)
[2] => Array
(
[name_type] => DNSDomain
[name] => mydnsdomain.com
)
)
The problem is that these arrays are never in the same order and the keys are named the same. I only need the DNSFQDN. When I loop through the array how can I check to see if the DNSFQDN value is there, and then retrieve charles.mydnsdomain.com so I can put it into a varible.
I've tried functions like in_array and array_search but I'm having trouble with these as I'm working strictly with associative arrays.
Any help would be appreciated.
This will be helpful;
$input = array(
array(
'name_type' => 'UnixName',
'name' => 'charles'
),
array(
'name_type' => 'DNSFQDN',
'name' => 'charles.mydnsdomain.com'
),
array(
'name_type' => 'DNSDomain',
'name' => 'mydnsdomain.com'
)
);
$domain = $input[array_search('DNSFQDN', array_column($input, 'name_type'))]['name'];
echo $domain; exit;
// output; charles.mydnsdomain.com
its simple try bellow ..i hope it will help
<?php
$test_array = array(
array('name_type'=>'UnixName','name'=>'charles'),
array('name_type'=>'DNSFQDN','name'=>'charles.mydnsdomain.com'),
array('name_type'=>'DNSDomain','name'=>'mydnsdomain.com'),
);
foreach ($test_array as $key => $value) {
if($value['name_type']=='DNSFQDN'){
echo "Domain Name :";
echo $value['name'];
}
}
?>
You have to iterate array using foreach loop.
<?php
$array_values = array(
array('name_type'=>'UnixName','name'=>'charles'),
array('name_type'=>'DNSFQDN','name'=>'charles.mydnsdomain.com'),
array('name_type'=>'DNSDomain','name'=>'mydnsdomain.com'),
);
foreach ($array_values as $value) {
if($value['name_type']=='DNSFQDN'){
echo "DNSDomain = ".$value['name'];
}
}
?>

select data from array that matches condition in another array in php

i have 2 arrays i want to display the final array as what are the array element in $displayArray only be displayed from the $firstArray
$firstArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_Desc] => Discount
[Dis_Per] => 7.500
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_Desc] => Discount
[Dis_Per] => 2.500
[Dis_val] => 8.13
)
)
$displayArray = Array
(
[0] => Array
(
[0] => Dis_id
[1] => Dis_val
)
)
i want the final output will be
$resultArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_val] => 8.13
)
)
Both the $firstArray and the $DisplayArray are dynamic but the $displayArray should be one.
i dont know how to do give me any suggestion
First up, if $displayArray will never have more than one array, the answer is pretty simple. Start by popping the inner array, to get to the actual keys you will need:
$displayArray = array_pop($displayArray);//get keys
$resultArray = array();//this is the output array
foreach ($firstArray as $data)
{
$item = array();
foreach ($displayArray as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;//make sure the key exists!
$resultArray[] = $item;
}
var_dump($resultArray);
This gives you what you need.
However, if $displayArray contains more than 1 sub-array, you'll need an additional loop
$resultArray = array();
foreach ($displayArray as $k => $keys)
{
$resultArray[$k] = array();//array for this particular sub-array
foreach ($firstArray as $data)
{
$item = array();
foreach ($keys as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;
$resultArray[$k][] = $item;//add data-item
}
}
var_dump($resultArray);
the latter version can handle a display array like:
$displayArray = array(
array(
'Dis_id',
'Dis_val'
),
array(
'Dis_id',
'Dis_desc'
)
);
And it'll churn out a $resultArray that looks like this:
array(
array(
array(
'Dis_id' => 'foo',
'Dis_val' => 123
)
),
array(
array(
'Dis_id' => 'foo',
'Dis_desc' => 'foobar'
)
)
)
Job done

php: getting only top value in multidimensional array

My nested array looks like:
[Minion] => Array
(
[old_first_name] => "\345\205\265"
[old_last_name] => "\345\274\265"
[old_name] => "\345\205\265\345\274\265"
)
[Evil Minion] => Array
(
[old_first_name] => "\347\251\216"
[old_last_name] => "\345\274\265"
[old_name] => "\345\274\265\347\251\216"
)
[Minion 2] => Array
(
[old_first_name] => "\345\212\233"
[old_last_name] => "\345\274\265"
[old_name] => "\345\274\265\345\212\233"
)
How do I just get Minion, Evil Minion, and Minion 2?
I tried a for loop but it's just looping through the contents of Minion which isn't what I want!
Use the array_keys function:
$keys = array_keys($array);
var_dump($keys);
This works for any array, whether it's one-dimensional or multi-dimensional.
Do this way.. you need to nest furthermore
<?php
$arr= array(
'Minion' => Array
(
'old_first_name' => "\345\205\265",
'old_last_name' => "\345\274\265",
'old_name' => "\345\205\265\345\274\265"
),
'Evil Minion' => Array
(
'old_first_name' => "\347\251\216",
'old_last_name' => "\345\274\265",
'old_name' => "\345\274\265\347\251\216"
),
'Minion 2' => Array
(
'old_first_name' => "\345\212\233",
'old_last_name' => "\345\274\265",
'old_name' => "\345\274\265\345\212\233"
)
);
foreach($arr as $arr1)
{
foreach($arr1 as $k=>$v)
{
echo "$k => $v";
}
}
Demo
You'd be interested in array_keys to just fetch the keys...
$keys = array_keys($arr);
Since this returns an array of the keys, you may further loop through it using a for-each construct.

multi-dimensional array into a single layered array PHP

I have the following array being returned
Array
(
[0] => Array
(
[uid] => 616941445
)
[1] => Array
(
[uid] => 1354124203
)
)
However I want just a single layered array, so i would like something like this.
Array
(
[0] => 616941445
[1] => 1354124203
)
foreach ($arr as $key => $val) {
$arr[$key] = $val['uid'];
}
<?php
$multi_arr = array(
array(
'uid' => 616941445
),
array(
'uid' => 1354124203
),
);
$single_arr = array();
foreach($multi_arr as $arr){
foreach($arr as $val) $single_arr[] = $val;
}
?>
As always, when you need to change two level array into one level without preserve keys:
$your2DArray = array(/* .. */);
$flatArray = array_map('array_pop', $your2DArray);
And like you want to, no loops.
foreach($arr as $key=>$val) {
$single_arr[] = $arr[$key]['uid'];
}

Categories