Remove certain String from array php - php

I want to get rid of that string with help of the str_replace function in PHP but I won't work. Can anybody help me?
$sql = "SELECT data from users";
$result = $mysqli->query($sql);
$widgets = [];
while($row = mysqli_fetch_array($result))
{
$widgets[] = $row;
}
$alignment = explode("//",serialize($widgets));
for($i = 0; $i<count($alignment); $i++){
str_replace("a:3:{i:0,:a:2:{:i:0;s:31;q}")
}
?>

As said in http://php.net/manual/en/function.str-replace.php
php_replace take 3 arguments
$replaced = str_replace(search, replaced_by, initial_string, [&replaced]);

Refer str_replace function for clarity, and change your code like below:
$replacedValue = str_replace("a:3:{i:0,:a:2:{:i:0;s:31;q}","",$alignment[$i]);

Related

php create array with my sqli query and not array of objects

I'm trying to make an array with the result of a mysql query but instead I'm making an array of objects.
Have unsuccessfully tried to convert the array of objects into an array, is that the best approach?
echo json_decode(json_encode($iconsArray), true); // didnt work
This is my query
function listIcons(){
global $link;
$queryAssets = "SELECT DISTINCT icon_1,icon_2,icon_3,icon_4 FROM icons";
$resultqueryAssets = mysqli_query($link, $queryAssets) or
die(mysqli_error($link));
while ($row = mysqli_fetch_assoc($resultqueryAssets)) {
$iconsArray[] = $row;
}
$allIcons = json_encode($iconsArray);
echo $allIcons;
return array($allIcons);
}
allIcons outout is
$allIcons = [
0:{icon_1: "01_a", icon_2: "01_b", icon_3: "01_c", icon_4: "01_d"}
1{icon_1: "02_a", icon_2: "02_b", icon_3: "02_c", icon_4: "02_d"}
]
And the output that I need but cant achieve
$allIcons = [
0:"01_a"
1:"02_a"
2:"01_b"
3:"02_b"
4:"03_a"
5:"02_b"
...
]
Most likely this is the easiest thing to do:
<?php
// ....
$queryAssets = "SELECT DISTINCT icon_1,icon_2,icon_3,icon_4 FROM icons";
$resultqueryAssets = mysqli_query($link, $queryAssets) or
die(mysqli_error($link));
while ($row = mysqli_fetch_assoc($resultqueryAssets)) {
foreach ($row as $icon) {
$icons[] = $icon;
}
}
var_dump($icons);
An alternative without an additional loop would be such thing:
<?php
// ....
$queryAssets = "SELECT DISTINCT icon_1,icon_2,icon_3,icon_4 FROM icons";
$resultqueryAssets = mysqli_query($link, $queryAssets) or
die(mysqli_error($link));
$icons = [];
while ($row = mysqli_fetch_assoc($resultqueryAssets)) {
$icons = array_merge($icons, array_values($row));
}
var_dump($icons);
Actually things get easier if you don't fetch an associative array at all:
<?php
// ....
$queryAssets = "SELECT DISTINCT icon_1,icon_2,icon_3,icon_4 FROM icons";
$resultqueryAssets = mysqli_query($link, $queryAssets) or
die(mysqli_error($link));
$icons = [];
while ($row = mysqli_fetch_row($resultqueryAssets)) {
$icons = array_merge($icons, $row);
}
var_dump($icons);
I did not run these code snippets, just typed them down. I hope there is no silly typo in them...

Dynamic Content in PHP array

This should be a quick one.
I'm pulling a list of id's and I need to place them in an array.
Here is my php code to get the list of id's
$get_archives = mysql_query("SELECT * FROM archive WHERE user = '$email' ");
while ($row = mysql_fetch_assoc($get_archives)) {
$insta_id = $row['insta_id'];
$insta_id = "'" . $insta_id."',";
echo $insta_id;
};
This echo's a list of id's that looks like this: '146176036','136514942',
Now I want to put that list into an array. So i tried something like this:
$y = array($insta_id);
However that isn't working. Any suggestions?
$y = array();
while ($row = mysql_fetch_assoc($get_archives)) {
$y[] = $row['insta_id'];
}
$myArray = array();
$get_archives = mysql_query("SELECT * FROM archive WHERE user = '$email' ");
while ($row = mysql_fetch_assoc($get_archives)) {
$insta_id = $row['insta_id'];
$insta_id = "'" . $insta_id."',";
$myArray[] =$insta_id;
};
did you mean like this: ?
$insta_id=array();
while ($row = mysql_fetch_assoc($get_archives)) {
$insta_id[] = $row['insta_id'];
}
Create an array, and push the values into it:
$values = array();
while ( $row = mysql_fetch_assoc( $get_archives ) ) {
array_push( $values, $row['insta_id'] );
}

Proper PHP MYSQL array usage

I have not worked with arrays much in PHP. I have a table of colors. I want to load it into a multidimensional associative array because I am going to be using that table a lot and don't want to do selects over and over again.
I did:
$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = "";
while($colorrec = mysql_fetch_array($result)){
$colors[$colorrec['ID']][0] = $colorrec['Description'];
$colors[$colorrec['ID']][1] = $colorrec['HexCode'];
}
then when I want to access a colors information, I can just do something like:
echo "color code WHT";
echo "description ".$colors['WHT'][0];
echo "Hex Code ".$colors['WHT'][1];
Is this the correct way/methodology to do this?
$colors = ""; //what?? this should be array()
This should be something like:
$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = array();
while($colorrec = mysql_fetch_array($result)){
$colors[$colorrec['ID']] = array();
$colors[$colorrec['ID']]['Desc'] = $colorrec['Description'];
$colors[$colorrec['ID']]['Hex'] = $colorrec['HexCode'];
}
Then you can do:
echo "color code WHT";
echo "description ".$colors['WHT']['Desc'];
echo "Hex Code ".$colors['WHT']['Hex'];
Sure nothing wrong with going this way. Also if you get lost in your array, just do a quick print_r($array) and it will output the structure for you.
try with:
$result = mysql_query("select * FROM color") or die(mysql_error());
$colors = array();
while($colorrec = mysql_fetch_array($result)){
$colors[$colorrec['ID']] = array(
$colorrec['Description'] ,
$colorrec['HexCode'] );
}
echo "color code WHT";
echo "description ".$colors['WHT'][0];
echo "Hex Code ".$colors['WHT'][1];
Neal you can do as he mentioned, if you want more clean go OOP (PHP5)
class Color
{
$description ="";
$hexcode = "";
$someOther="";
}
$colors = array();
$result = mysql_query("select * FROM color") or die(mysql_error());
while($colorrec = mysql_fetch_array($result))
{
$color = new Color();
$id=$colorrec['HexCode'];
$color->description = $colorrec['Description'];
$color->hexCode = $colorrec['HexCode'];
$colors[$id] = $color;
}
echo $color['ID']->description;
echo $color['ID']->hexCode;
I use it like this:
$numfields = mysql_num_fields($result);
while($red = mysql_fetch_array($result)){
for ($j=0; $j<$numfields; $j++ ) {
$field_name = mysql_field_name($result, $j);
//create Matrix
$datapull[$i][$field_name] = $red[$field_name];
}
$i++;
}
mysql_free_result($result);
var_dump($datapull);
This is more generic and harder to search, it should be used in class method or a function, so you pass the SQL and always expect the same format as a result.

How to index the result of a mySql query as an array of array?

If I need to select and use information of every element of a table in a database the procedure would be this:
$query = "...mySql query...";
$query_result = mysql_query($query) or die (mysql_error());
Then if I wished to access the fields of the result I would use the function mysql_fetch_array() and access them like this:
$query_result_array = mysql_fetch_array($query_result);
echo $query_result_array['field_1'];
....
echo $query_result_array['field_i'];
....
But since more elements could be returned by the query I would like to access every single of them with an array indexed from 0 to mysql_num_rows($query_result).
As an example:
echo $query_result_array['field_i'][0];
....
echo $query_result_array['field_i'][mysql_num_rows($query_result)];
should print for every selected element of the table the value of field i.
Is there a function that will do the job for me?
If not, any suggestions on how to do it?
Thanks in advance for help.
This may be an alternative
$res = mysql_query("..SQL...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$arr[] = $row;
}
var_dump($arr);
Or
$res = mysql_query("..SQL...");
for
(
$arr = array();
$row = mysql_fetch_assoc($res);
$arr[] = $row
);
var_dump($arr);
I don't think there is such a method; you have to do it yourself.
try with something like:
$res = mysql_query("..mySql query...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$query_result_array[] = $row;
}
then you access your data like:
echo $query_result_array[0]['field_i'];
based on 2 previous answers, those authors assuming that usual SO author is familiar with such a thing as creating a function
function sqlArr($sql) { return an array consists of
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
if ($res) {
while ($row = mysql_fetch_assoc($res)) {
$ret[] = $row;
}
}
return $ret;
}
$array = sqlArr("SELECT * FROM table");
foreach ($array as $row) {
echo $row['name'],$row['sex'];
}
this resulting array have different structure from what you asked, but it is way more convenient too.
if you still need yours unusual one, you have to tell how you gonna use it

format mysql data in array

I am pulling data from my database and trying to encode into JSON data using json_encode. But to make it easier to read in my android app. I was hopping to format it differently then I am currently doing. Please see bottom encode string example. Any help would be great. Thanks in Advance.
$result = $db->query($query);
while($info = mysql_fetch_array($result))
{
$content[] = $info;
}
$count = count($content);
$result=array();
for($i=0;$i<$count;$i++)
{
$result[id][] = $content[$i]['imageID'];
$result[name][] = $content[$i]['Name'];
$result[thumb][] = $content[$i]['Thumb'];
$result[path][] = $content[$i]['Path'];
}
echo json_encode($result);
{"id":["1","2","3"],"name":["Dragon","fly","bug"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}
But I am trying to format my array like so when it is encoded by json_encode.
[{"id":"1","name":"Dragon","thumb":"thm_polaroid.jpg","path":"polaroid.jpg"},{"id":"2","name":"Fly","thumb":"thm_default.jpg","path":"default.jpg"},{"id":"3","name":"Bug","thumb":"thm_enhanced-buzz-9667-1270841394-4.jpg","path":"enhanced-buzz-9667-1270841394-4.jpg"}]
Well, there is a problem. This is not valid JSON:
{"image":["1","Dragon","thm_polaroid.jpg","polaroid.jpg"],
"image":["2","fly","thm_default.jpg","default.jpg"]}
A JSON object can only have one value per unique key. This means that your latter image key would clobber the value of the former.
If you are content with this, however:
[["1","Dragon","thm_polaroid.jpg","polaroid.jpg"],
["2","fly","thm_default.jpg","default.jpg"]]
Then you can simply use mysql_fetch_row:
$result = $db->query($query);
while($info = mysql_fetch_row($result))
{
$content[] = $info;
}
echo json_encode($content);
Side Note:
Generally, in PHP, it is best to use foreach( $arr as $val ) (or $arr as $key => $val). for loops should be limited to when they are strictly necessary.
You need to add the iterator $i to the setting array
for($i=0;$i<$count;$i++)
{
$result[$i][id] = $content[$i]['imageID'];
$result[$i][name] = $content[$i]['Name'];
$result[$i][thumb] = $content[$i]['Thumb'];
$result[$i][path] = $content[$i]['Path'];
}
<?
$result = $db->query($query);
while($info = mysql_fetch_array($result))
$content[] = $info;
$result=array();
$count = count($content);
for ($x=0;$x<$count;++$x)
{
$result[$x][] = $content[$x]['imageID'];
$result[$x][] = $content[$x]['Name'];
$result[$x][] = $content[$x]['Thumb'];
$result[$x][] = $content[$x]['Path'];
}
echo json_encode($result);
?>

Categories