Getting a value from an associative array. - php

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'];
}
}
?>

Related

Convert and rewrite an array

I need to convert my array but I don't have extended experience to complete this task.
Please help me to find a way to do please?
I have this:
Array(
[0] => Array
(
[BTC] => 0.07634
)
[1] => Array
(
[ETH] => 0.00103
)
[2] => Array
(
[LTC] => 0.006787
)
[3] => Array
(
[XMR] => 0.006351
)
And I need this:
Array(
[BTC] => 0.07634
[ETH] => 0.00103(
[LTC] => 0.006787
[XMR] => 0.006351
[ZEC] => 0.00144
[MD_DT_CAD] => 2017-08-14 02:16:44
)
You have following
$data =array(
array("BTC" => 0.07634),
array("ETH" => 0.00103),
array("LTC" => 0.006787),
array("XMR" => 0.006351)
);
You can achieve your result by following.
<?php
$data =array(
array("BTC" => 0.07634),
array("ETH" => 0.00103),
array("LTC" => 0.006787),
array("XMR" => 0.006351)
);
foreach($data as $value){
foreach ($value as $key => $value1) {
$new_arr[$key] = $value1;
}
}
echo "<pre>";
print_r($new_arr);
?>
Simple use call_user_func_array with array_merge
$array = Array("0" => Array("BTC" => 0.07634),"1" => Array("ETH" => 0.00103),"2" => Array("LTC" => 0.006787),"3" => Array("XMR" => 0.006351));
$new_array = call_user_func_array('array_merge', $array);
print_r($new_array);
<?php $array=array(array("BTC" => 0.07634),array("ETH" => 0.00103),array("LTC" => 0.006787),
array("XMR" => 0.006351));
//echo print_r($array);
$array2 = array_reduce($array, 'array_merge', array());//or call_user_func_array('array_merge', $array);
echo print_r($array2);
?>
Assuming your array is called $array :
$new_array = array_merge(... $array);
Explaination : array_merge() takes an undefined amount of different arrays as parameters, gathers them as entries in a single array by using the splat operator (...) and then merges all these arrays in one before returning it.
Calling that function and passing it a single array and using the splat operator in the calling too makes that single array to be the single array containing the arrays to merge on which the function will work. By doing that, you can have the function to merge sub_arrays of an array you already have without calling additional functions.

Codeigniter PHP create two different array from one

I'm newbie of codeigniter and PHP.
Can I separate an array into two different arrays?
This is my $array:
Array (
[0] => Array
(
[Name] => mark
[Surname] => mark
)[1] => Array
(
[Name] => greg
[Surname] => greg
)
)
Is it possible to create an array of $mark and another with $greg?
If you want to use the value of Name as your variable name: Variable variables
foreach ($arrays as $array) {
if (isset($array['Name'])) {
$$array['Name'] = $array;
}
}
print_r($mark);
You may use eval() if you want to set a string value and make it a variable.
<?php
$arrays = array(
array(
'Name' => 'mark',
'Surname' => 'mark'
),
array(
'Name' => 'greg',
'Surname' => 'greg'
)
);
//I'd use foreach()
foreach ($arrays as $array) {
eval("$".$array['Name']." = array('Name'=>'{$array['Name']}','Surname'=>'{$array['Surname']}',);");
}
echo '<pre>';
var_dump($mark, $greg);
echo '</pre>';

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.

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 />";
}

PHP dealing with arrays

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'];
}

Categories