How to get a specific data from array? - php

Right now I have a problem with retrieving data form arrays, maybe it's really simple (probably it is) but I am struggling wiht it since morning and it appears, that my knowledge about PHP is worth nothing...so I have few arrays:
array( "name" => "Array 1",
"type" => "array"),
array( "name" => "Array 2",
"type" => "whatever"),
array( "name" => "Array 3",
"type" => "whatever"),
array( "name" => "Array 4",
"type" => "array"),
array( "name" => "Array 5",
"type" => "whatever"),
What I need to do is to display 'name' of arrays of the 'array' type, I know I need a foreach loop but how to construct 'foreach ($arrays as $array) {' to get the desired result?
EDIT
Thanks for all the replies, but I tihnk I didn't make myself clear. I need to display "name" only when there is a "type" => "array" present within array, every other arrays' "name" should be omitted.

You'll need to do something like this:
foreach($arrays as $array) {
if($array['type'] == 'array') {
print($array['name']);
}
}

An array_map will do
array_map(function ($v){ if($v['type']=="array"){echo $v['name']."<br>";}},$arr);
OUTPUT :
Array 1
Array 4
Demo

put the arrays into a "container" array first, then you can use that one...
<?php
$data = array(
array( "name" => "Array 1",
"type" => "array"),
array( "name" => "Array 2",
"type" => "whatever"),
array( "name" => "Array 3",
"type" => "whatever"),
array( "name" => "Array 4",
"type" => "array"),
array( "name" => "Array 5",
"type" => "whatever")
);
foreach($data as $array) {
if($array['type'] == 'array') {
print($array['name']);
}
}

Related

Remove specific string from multidimensional array by value, not key

I need to remove Jackie Jackson from this array, I tried unset(I do not want to use key), array_diff, array_search. Nothing is working for me.
$employeeList= array(
array(
"ID" => "ID",
"Name" => "Name",
"Surname" => "Surname",
),
array(
"ID" => 1,
"Name" => "John",
"Surname" => "Smith",
),
array(
"ID" => 2,
"Name" => "Jackie",
"Surname" => "Jackson",
),
array(
"ID" => 3,
"Name" => "Chris",
"Surname" => "Jones",
),
array(
"ID" =>4,
"Name" => "Amanda",
"Surname" => "Cullen",
),
array(
"ID" =>5,
"Name" => "Jeremy",
"Surname" => "Goodwin",
),
);
if you want to unset you can use the id of Jackie Jackson to match his key.
$id = 2;
unset($employeeList[array_search($id,array_column($employeeList, "ID"))]);
Have fun :)
Array filter to check if the name and surname match. The string must contain name and surname separated by a space in this case so we can "explode" and pick up each individually. If name or surname do not exist in the "name_to_exclude" the function will simply return the original array.
$name_to_exclude = "Jackie Jackson";
$exclude = explode(' ', $name_to_exclude);
$employeeList = array_filter($employeeList, function($val) use ($exclude) {
if(array_key_exists(0, $exclude) && array_key_exists(1, $exclude)) {
return $val['Name'] != $exclude[0] && $val['Surname'] != $exclude[1];
}
return true;
});

Map two dimensional php array to 1 dimension

I have array inside array:
{
"0" => array("key" => "code", "id" => "4", "value" => "yes"),
"1" => array("key" => "parameter", "id" => "4", "value" => "0"),
"2" => array("key" => "code", "id" => "5", "value" => "no"),
etc...
}
This is what I want to do: I want to have one dimension array in which key would be "id" and value would be "value". However, I need to filter out entries whose key is "parameters". So, in this example, the final array should look like this:
{
"4" => "yes",
"5" => "no"
}
I just can't seem to figure out how to do this. Could you please help me a bit? I tried writing this foreach inside foreach but I just can't wrap my head around how to filter data.
foreach ($settings AS $key => $value) {
$id = null;
$value = null;
foreach ($value AS $key2 => $value2) {
// No idea how to filter out uneccesary entries and save the correct ones
}
$finalArray[$id] = $value;
}
This should do it :
$finalArray = array();
foreach ($settings as $setting) {
if ($setting['key'] != 'parameter') {
$finalArray[$setting['id']] = $setting['value'];
}
}
Assuming all your entries have keys 'key', 'id' and 'value'.
use array_column and array_filter like this, if you want to filter more keys add them to out_keys array :
<?php
$array = [
["key" => "code", "id" => "4", "value" => "yes"],
["key" => "parameter", "id" => "4", "value" => "0"],
["key" => "code", "id" => "5", "value" => "no"]
];
$out_keys = ['parameter'];
$result = array_column(array_filter($array, function($item) use($out_keys) {
return !in_array($item['key'], $out_keys);
}), 'value', 'id');
echo "<pre>";
print_r($result);
output:
Array
(
[4] => yes
[5] => no
)
Assuming $data is your starting array, the code below will output what you want in $result
$result = [];
foreach(array_filter($data, function($el){return $el['key']!='parameter';}) as $el){
$result[$el['id']] = $el['value'];
}
Live demo

Access different arrays with similar keys

I have this array
<?php
$themename = "So";
$shortname = "se";
$options = array (
array( "name" => "the_firstname",
"desc" => "The firstname of a person",
"id" => $shortname."_the_firstname",
"type" => "text",
"value" => ""),
array( "name" => "the_lastname",
"desc" => "A persons lastname",
"id" => $shortname."_the_lastname",
"type" => "text",
"value" => ""),
);
foreach($options as $key => $value)
{
echo $value['id']."<br/>";
}
?>
with similar keys for instance the id key.I would like to access the id value of the first array.
Doing this echo $value['id'][0]."<br/>"; or echo $options['id'][0]."<br/>"; isn't helping.
How can i show the value of the first id?.
echo $options[0]['id']; should do the trick on top level, inside the for loop this should work: $value['id']. You don't need the [0] here, since $value contains the first (0) element inside the $options array.

Get repeated key => values in every array within a multiarray + other no repeated key = > values

i have this multi array "$marray wich has inside some array with some similar key = value and some not look like this :
$marray = array(
array("id" => "1", "be_pro" => 6, "name" => "a1", "service" => 4a),
array("id" => "2", "be_pro" => 6, "name" => "a1", "service" => 4d),
array("id" => "3", "be_pro" => 4, "name" => "a4", "service" => 3d),
array("id" => "4", "be_pro" => 4, "name" => "a4", "service" => 3s),
array("id" => "6", "be_pro" => 4, "name" => "a4", "service" => 34),
array("id" => "8", "be_pro" => 3, "name" => "a3", "service" => 4r),
array("id" => "8", "be_pro" => 3, "name" => "a3", "service" => 4d)
);
So i would like to get new arrays with "id", "be_pro" and "name" once then "service" plus "service" from next array till "be_pro" in the new array is different , so if is different put in the next array.
How should i do this?
what i need is print a multi array and within and array with every row with similar be_pro
This help me:
// first get all bepro_id and put in array
foreach($all_rows as $service){
$bepros[$service['bepro_id']]++;
}
//var_dump($bepros);
//Then for each be pro_id print every row , so print every service , then for be pro_id,id,name just print the first key "[0]" the array inside $new_array
foreach ($bepros as $key=>$bepro){
if(isset($new_bep)){
$x = 0 + $new_bep;
}else{
$x = 0;
}
$new_array[] = array_slice($all_rows,$x,$bepro);
// get where let before
$new_bep=$x + $bepro;
}

PHP multidimensional array

Here's a quickie for the pros:
How do I display Value 1, 2, 3 etc as it's in it's third array?
$meta_boxes = array
(
"checkbox" => array
(
"name" => "checkbox",
"title" => "",
"description" => "This is an example of a checkbox field.",
"type" => "checkbox",
"rows" => "",
"width" => "",
"options" => array
(
"1" => "Value 1",
"2" => "Value 2",
"3" => "Value 3"
)
),
$str = '';
foreach($meta_boxes['checkbox']['options'] as $k => $v) {
$str .= $v . "\n";
}
$meta_boxes['checkbox']['options']['1'] will give you the string "Value 1" from the array, and then you can do whatever you want with that value.
You should now be able to figure out how to access the other two.

Categories