Split on multiple instances of word in string [duplicate] - php

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Parse JSON string contents into PHP Array
(4 answers)
Closed 5 years ago.
I have strings like this:
$T = '[{"X":"13","Y":"76", "R":"90"},{"X":"12","Y":"24","R":"-19"}]}';
$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
{"X":"12","Y":"24","R":"-19"}]}';
I want to find each X value in the string, and get the value after them.
Note that the number of X values differs.
For example, I want:
13, 12, 1, 1, 12
I tried explode(), but that only grabbed the first X value (13). I am new to PHP, and don't know an elegant solution. Any suggestions are appreciated.

Remove the last curly brace (}) of your strings to have valid json, i.e:
$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
{"X":"12","Y":"24","R":"-19"}]}';
$A = trim($A, '}');
$json = json_decode($A, true);
foreach($json as $value)
{
echo $value['X'];
}
# 1112

Related

Filter array by skipping every 2nd and 3rd element from array [duplicate]

This question already has answers here:
Selecting every nth item from an array
(9 answers)
Remove every 3rd and 4th element from array in php
(1 answer)
How to access N-th element of an array in PHP
(3 answers)
Remove every nth item from array
(6 answers)
foreach step 5 steps forward in an array
(3 answers)
Closed 4 months ago.
i have a array like this
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
what i wan to remove
$remove = "b,c,e,f,h,i,k,l";
then i need a new array from the remaining elements like below
$new_arr = [a,d,g,j,m];
Use array chunk to split by 3 and take out first element.
<?php
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
$chunked =array_chunk($names, 3);
$filtered = [];
foreach($chunked as $chunk){
$filtered[] = $chunk[0];
}
var_dump($filtered);
?>
Instead of removing number 2 and number 3 from each 3 elements, the task is simply written like this:
Keep the first of every 3 items.
This can be determined using the index and the modulo operator %.
Using array_filter saves the foreach loop. This allows the solution to be implemented as a one-liner.
$names = ['a','b','c','d','e','f','g','h','i','j','k','l','m'];
$result = array_filter($names,fn($k) => $k%3 == 0, ARRAY_FILTER_USE_KEY );
var_dump($result);
Demo: https://3v4l.org/sKTSQ
The $names array needs to be noted as in the code above. A notation without quotes like in the question produces error messages.

Laravel WhereIn array returns only first index results [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 10 months ago.
$dean_ids = Auth::user()->dean_id; // "9,11"
$subjects = Subject::whereIn('dean_id', [$dean_ids])->select('id')->get();
returns only data for "9" but when i trying like this:
$subjects = Subject::whereIn('dean_id', [9,11])->select('id')->get();
//it returns all data that what i want.
how can i fix it?
As I see, this line $dean_ids = Auth::user()->dean_id; returns a comma-separated string. So when you make $dean_ids array by using [$dean_ids] it actually makes an array like:
array(
'9,11'
)
Instead of
array(
9,
11
)
There is only one value inside the array. So what you can do just use explode for splitting the string by using a comma and it also returns an array.
You can try this:
$subjects = Subject::whereIn('dean_id', explode(',', $dean_ids))->select('id')->get();

PHP: Find highest index of numeric array that has missing elements [duplicate]

This question already has answers here:
Search for highest key/index in an array
(7 answers)
Closed 4 years ago.
Say I have the following array:
$n[5] = "hello";
$n[10]= "goodbye";`
I would like to find out the highest index of this array.
In Javascript, I could do $n.length - 1 which would return 10.
In PHP, though, count($n) returns 2, which is the number of elements in the array.
So how to get the highest index of the array?
Use max() and array_keys()
echo max(array_keys($n));
Output:-https://eval.in/997652
$n = [];
$n[5] = "hello";
$n[10]= "goodbye";
// get the list of key
$keyList = array_keys($n);
// get the biggest key
$maxIndex = max($keyList);
echo $n[$maxIndex];
output
goodbye

Easy way to parse array values [duplicate]

This question already has answers here:
Applying a function for each value of an array
(5 answers)
Closed 6 years ago.
Is there util that parses array content without the need of iterating it and parsing each value?
input: ['2','3','7']
output: [2, 3, 7]
This obviously iterates internally, but you don't have to code an iterator:
$output = array_map('intval', $input);
Maps every value in $input to the intval() function and returns the result. For things that cannot be converted into an integer you'll get 0 or for objects, a notice and that value will not be returned.
I can't tell if you want to remove 0 values or not from your comment, but if so:
$output = array_filter(array_map('intval', $input));
You can use array_map
array = ["2","3","4"];
$intArray = array_map('intval', $array);

convert string to array and get value in php [duplicate]

This question already has answers here:
PHP string to array
(4 answers)
Closed 8 years ago.
I have a complete string how can i get some part of it and insert into an array this is my string in php
[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]
i want something similar to this
$value1 = $array[0];
// {"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"}
is this possible to get each value like this
$value1 = $array[0]['albumid'];
// ASaBFzCtl8
Yes use json_decode()
$j = '[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]';
$data = json_decode($j,true);
You can use loop to read the data as
foreach($data as $key=>$val){
echo $val["albumid"]."<br />";
}
Above will just get the albumid you can read whatever you want from this array.

Categories