PHP search an array value inside an array in one line [duplicate] - php

This question already has an answer here:
Get key from first array on Search Array in Array PHP
(1 answer)
Closed 3 months ago.
I'm using PHP 7.4. I have this array :
$sections = [
'sectionOne' => [
'foo',
'bar',
'hello',
],
'sectionTwo' => [
'yo',
'heya',
],
];
I'd like to build a function to return the section of the received array value
public function getSectionByValue($value) {
return ...
}
If the value is bar then I'll get sectionOne. If the value is yo then I'll get sectionTwo etc...
How can I do to search an array value inside an array ? It is possible to do this in one line ?

You can do something like this inside getSectionByValue function if you really want an one liner.
return key(array_filter($sections, fn($section) => in_array($value, $section)));
If the provided value exists in multiple sections, it will just return the first one.

Related

Group data by first element of 2 dimensional array - PHP [duplicate]

This question already has answers here:
Prevent array overwriting and instead create new array index
(2 answers)
Closed 2 years ago.
I have this code.
$shirts = [
['xl','silk','blue'],
['xl', 'cotton', 'red'],
['L', 'silk', 'green'],
['L', 'silk', 'black']
];
I want to group size . For example, I want output like this
$shirtbysize = [
'xl' =>
[
['silk','blue'],
['cotton','red']
],
'L' =>
[
['silk','green'],
['silk','black']
],
];
I searched everywhere but I didn't get answer. Can anyone tell me how to do this?
Use array_shift to pick the first element and remove it from array.
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be affected.
Use array_shift to pick the first element and set it as index.
Now first element is already removed from first step, set the remaining array as data to above index.
$shirtbysize = [];
foreach($shirts as $shirt){
$shirtbysize[array_shift($shirt)][] = $shirt;
}

What does (array) with two parentheses like this mean in PHP? [duplicate]

This question already has answers here:
How to cast variable to array
(8 answers)
Closed 4 years ago.
I've been reading some code recently and I've come across a few instances where the developer would wrap parentheses around like following example.
There's this function:
foreach ((array) $middleware as $m) {
$this->middleware[] = [
'middleware' => $m,
'options' => &$options,
];
}
In the for each check there's array (array) with parentheses around, what do these mean.
Or can someone guide me to a doc where I can read up on this matter.
Thanks in advance.
This is called type casting.
It forces $middleware to an array data type. Some examples:
(array) [1,2] // [1,2]
(array) 1 // [1]
(array) stdClass{hello: world} // [hello => world] (object to array pseudocode)

how to use array_push() [duplicate]

This question already has answers here:
Add values to an associative array in PHP
(2 answers)
Closed 7 years ago.
I want to push new data in array which each value of them.
$array = array("menu1" => "101", "menu2" => "201");
array_push($array, "menu3" => "301");
But I got an error syntax.
And if I use like this :
$array = array("menu1" => "101", "menu2" => "201");
array_push($array, "menu3", "301");
result is : Array ( [menu1]=>101 [menu2]=>201 [0]=>menu3 [1]=>301 )
My hope the result is : Array ( [menu1]=>101 [menu2]=>201 [menu3]=>301 )
I want push new [menu3]=>'301' but I dont know how. Please help me, the answer will be appreciate
You can use
$array["menu3"] = "301"
as for array_push
array_push() treats array as a stack, and pushes the passed variables onto the end of array
so for associative arrays is a no match
another suitable function for what you want but it requires an array argument is array_merge
$result = array_merge(array("one" => "1"), array("two" => "2"));

Why is this PHP array not the same? [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 4 months ago.
I'm not understanding why the array:
<? $data = array( 'items[0][type]' => 'screenprint'); ?>
Is not the same as
<? echo $data['items'][0]['type']; ?>
I'm trying to add to the array but can't seem to figure out how?
array( 'items[0][type]' => 'screenprint')
This is an array which has one key which is named "items[0][type]" which has one value. That's not the same as an array which has a key items which has a key 0 which has a key type. PHP doesn't care that the key kinda looks like PHP syntax, it's just one string. What you want is:
$data = array('items' => array(0 => array('type' => 'screenprint')));
I hope it's obvious that that's a very different data structure.
It should be:
$data = [
'items' => [['type' => 'screenprint']]
];
echo $data['items'][0]['type'];

How to get key value of array? [duplicate]

This question already has answers here:
Getting the key of the only element in a PHP array
(6 answers)
Closed 8 years ago.
How do I get the value of a key of any array item? Like how a foreach loop turns it into $k => $v...except I only want to do that once, so no need for a loop. Do I really need to make a new array that it flips to?
Take this for example.
1 => array(
'street' => 'Street Address ',
'town' => 'Town/City '
),
2 => array(
'state' => 'State '
),
Those are arrays inside a bigger array. And now I tried to do this
array_flip($thatarrayupthere[2]['state'])
What I want to receive from that is "state" because that is the key name. But I'm getting errors.
I'm not exactly sure what you wan't, but if you just want to get the key of the second array in any given array this might help.
$key = key($array[2]);
In your example above you will get "state" in your $key variable.
$key = array_keys($array[2]);
print_r($key);
ref: http://php.net/manual/en/function.array-keys.php

Categories