How to Convert Range of Integers into PHP Array [duplicate] - php

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 4 years ago.
I'm trying to convert these integers/numbers:
$groups = '1707,1723,1733,16254,16256,18704,19792,29268,34956';
into an array in PHP array:
array("1707","1723","1733","29268","34956");
I'm using this
$tags = array();
foreach($groups as $index){
array_push($tags, $index);
}
But keep getting the below error.
Error: Invalid argument supplied for foreach()

simple explode call:
$array=explode(',',$groups);

The explode() function breaks a string into an array.
$groups = '1707,1723,1733,16254,16256,18704,19792,29268,34956';
print_r(explode(",",$groups));

Related

Array is not getting sorted with sort() function PHP [duplicate]

This question already has answers here:
Sort array not working
(2 answers)
Closed 10 months ago.
I'm trying to sort an array numerically. Here's my code
<?php
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
$expsort = sort($exp);
print_r($expsort);
?>
But it is not working. The output is showing only "1".
You are assigning the value of the sort function -which sorts the argument array itself, and it always returns true and thus you got 1 as a result.
So if you print your original exploded array, it will be sorted. Please note, sort overrides your original array
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
sort($exp);
print_r($exp);

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 string array to php array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have this string array:
["652","110","111","1032","118","416","147","121","112","1033","113","1031","868"]
I need to read each value so to get
652
110
111
1032
i try to convert string array using explode and then foreach but is not working...
$channels = explode('"', $string_array);
foreach($channels as &$channel) {
echo $channel.'<br>';
}
it's an JSON format, so use json_decode
$json = '["652","110","111","1032","118","416","147","121","112","1033","113","1031","868"]';
$array = json_decode($json, true);
foreach($array AS $channel) {
echo $channel.'<br>';
}

How to array_push unique values inside another array [duplicate]

This question already has answers here:
array_unique vs array_flip
(4 answers)
array_unique and then renumbering keys [duplicate]
(1 answer)
PHP arrays: delete duplicates and reorder keys [duplicate]
(2 answers)
array_unique does not re-sort the array
(2 answers)
Removing undefined array indexes after calling array_unique
(2 answers)
Closed 8 months ago.
I have two arrays:
$DocumentID = array(document-1, document-2, document-3, document-4,
document-5, document-4, document-3, document-2);
$UniqueDocumentID = array();
I want to push the unique objects inside of $documentid array to $UniqueDocumentID array.
I can't use array_unique() as it copies the key of its predecessor array and I want sequential keys inside the $UniqueDocumentID array.
You could foreach() through $DocumentID and check for the current value in $UniqueDocumentID with in_array() and if not present add it. Or use the proper tool:
$UniqueDocumentID = array_unique($DocumentID);
To your comment about wanting sequential keys:
$UniqueDocumentID = array_values(array_unique($DocumentID));
The long way around:
$UniqueDocumentID = array();
foreach($DocumentID as $value) {
if(!in_array($value, $UniqueDocumentID)) {
$UniqueDocumentID[] = $value;
}
}

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