I have the following question, let's say I'm querying MySQL database to get the following array:
Array ( [0] => Array ( [topicid] => 4 ) [1] => Array ( [topicid] => 5 ) [2] => Array ( [topicid] => 6 ) )
These are the results from that I get from the following SQL request
$sql=$db->query("SELECT topicid from opentopics WHERE userid=?","$userid")->fetchAll();
This query gives me the list of topics that are opened to certain user ids. Now there are several topics with IDs up to 3 that are open to everyone, so I want to use them also on my webpage. These topics are not opened in the opentopics table, but are made available to everyone based on their ID (less than 3).
So the question is following, how do I modify the multidimensional (i assume) array I get from MySQL request, how to add 3 new entries to the beghinning of the above mentioned array so it looks like:
Array ( [0] => Array ( [topicid] => 1 ) [1] => Array ( [topicid] => 2 ) [2] => Array ( [topicid] => 3 ) [3] => Array ( [topicid] => 4 ) [4] => Array ( [topicid] => 5 ) [5] => Array ( [topicid] => 6 ) )
As you may have notices in my text editory I have manually edited the text of the array, by adding this
[0] => Array ( [topicid] => 1 ) [1] => Array ( [topicid] => 2 ) [2] => Array ( [topicid] => 3 )
you can fetch your result like this example then use array_merge
$mysqli = new mysqli('localhost', 'root', '', 'test');
$manual = array(
[ 'id' => 9 , 'first_name' => 'John9' , 'last_name' => 'Doe9'] ,
[ 'id' => 19 , 'first_name' => 'John19' , 'last_name' => 'Doe19']
);
$sql = " SELECT * from users ";
$result = $mysqli -> query($sql);
$rows = $result -> fetch_all(MYSQLI_ASSOC);
$results = array_merge($manual , $rows);
the results will be like this
Array
(
[id] => 9
[first_name] => John9
[last_name] => Doe9
)
Array
(
[id] => 19
[first_name] => John19
[last_name] => Doe19
)
Array
(
[id] => 1
[first_name] => John
[last_name] => Doe
)
Array
(
[id] => 2
[first_name] => John3
[last_name] => Doe
)
Array
(
[id] => 4
[first_name] => John4
[last_name] => Doe
)
Related
I have an array from an inner join statement from 2 Mysql tables... I need them in a better working format for front end display:
mysql query:
SELECT weddings.id, weddings.wImage, images.iLink FROM images INNER JOIN weddings ON weddings.id = images.weddingId
OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image2.jpg
)
[1] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image3.jpg
)
[2] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => image4.jpg
)
[3] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image5.jpg
)
[4] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image6.jpg
)
[5] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => image7.jpg
)
[6] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image8.jpg
)
[7] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image9.jpg
)
[8] => Array
(
[id] => 11
[wImage] => image12.jpg
[iLink] => image10.jpg
)
)
So I need to work this array further to get this expected OUTCOME:
Array
(
[0] => Array
(
[id] => 3
[wImage] => image1.jpg
[iLink] => Array
(
[image] => image2.jpg
[image] => image3.jpg
[image] => image4.jpg
)
)
[1] => Array
(
[id] => 10
[wImage] => image11.jpg
[iLink] => Array
(
[image] => image5.jpg
[image] => image6.jpg
[image] => image7.jpg
)
)
[2] => Array
(
[id] => 10
[wImage] => image12.jpg
[iLink] => Array
(
[image] => image8.jpg
[image] => image9.jpg
[image] => image10.jpg
)
)
)
There are many ways, but assuming your id is unique (as I guess), you can use this id as an associative key in the output, so you can easily determine whether it exists already. In the last step you reindex the result array. See code comments for further explanation.
// $input_array is the outcome from sql
// $output_array will store the desired outcome
$output_array=[];
// loop through every item in input array
foreach ($input_array as $input_item) {
// if key with "id" already exists in output array
if (array_key_exists($input_item["id"], $output_array)) {
// append image to already existing "iLink" array
array_push($output_array[$input_item["id"]]["iLink"],
["image" => $input_item["iLink"]]
);
}
// if key with id not exists yet in output array
else {
// add current input item to output array,
// with "id" value as key
$output_array[$input_item["id"]] = [
"id" => $input_item["id"],
"wImage" => $input_item["wImage"],
// array with first image, can be appended later
"iLink" =>["image" => $input_item["iLink"]]
];
}
}
// now we have the result, but output array keys are not 0,1,2
// but the id values (3, 10 etc.), so we have to reindex
$output_array = array_values($output_array);
// now it is ready
var_dump($output_array);
I have trying to create a list of manufactures with a count.
This is my query
$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
FROM listed_watches
GROUP BY manufacture');
But when i do print_r, its duplicating the results. It is showing "manufacture" and "count" but its also showing [0] and [1], how come?
I just want it to show [manufacture], [count]
Array
(
[0] => Array
(
[manufacture] => Audemars Piguet
[0] => Audemars Piguet
[count] => 2
[1] => 2
)
[1] => Array
(
[manufacture] => Bell and Ross
[0] => Bell and Ross
[count] => 3
[1] => 3
)
[2] => Array
(
[manufacture] => Bulova
[0] => Bulova
[count] => 1
[1] => 1
)
)
try this :
$result = $query->fetchAll(PDO::FETCH_ASSOC);
You can use by default this fetching method in the initialization of your connection with :
$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
I'm having trouble modeling my database. Currently it looks like this (I'm hiding the irrelevant fields): http://i.imgur.com/SF9FzaD.png
It is working fine, for example, when I want it to return a list of all the servers:
$this->Server->find('all');
It returns an array with the right information:
Array
(
[0] => Array
(
[Server] => Array (...)
[User] => Array (...)
[Highlight] => Array
(
[0] => Array
(
[id] => 39
[id_server] => 8
[id_highlight] => 1
)
[1] => Array
(
[id] => 40
[id_server] => 8
[id_highlight] => 5
)
)
[SubServer] => Array(...)
)
[1] => Array
(
[Server] => Array (...)
[User] => Array (...)
[Highlight] => Array
(
[0] => Array
(
[id] => 41
[id_server] => 10
[id_highlight] => 4
)
[1] => Array
(
[id] => 42
[id_server] => 10
[id_highlight] => 5
)
)
[SubServer] => Array(...)
)
)
In short, each game "server" has some kind of higlights pointed by the user. Like "Anti-cheat system", "Active staff", "Custom events", etc. Each of these highlights have an id and a name.
Is there a way to grab the name from the table highlight_names corresponding to each of the highlights.id_hightlight (and the rest of the data, like the array above) using Model::find()?
I am using PEAR's PDO library to grab out the results I need.
Whilst I know I could just manually rebuild the array when the results are in, I was wondering is there a internal function I have missed that might do this for me when grabbing results directly from the DB?
Currently:
function get($query){
$affected =& $this->conn->query($query);
// Always check that result is not an error
if (PEAR::isError($affected)) {
die($affected->getMessage());
}
$res = $affected->fetchAll();
return $res;
}
Returns
[sexnumbers] => Array
(
[0] => Array
(
[sex] => 1
[no] => 35
)
[1] => Array
(
[sex] => 2
[no] => 17
)
[2] => Array
(
[sex] => 3
[no] => 3
)
)
Wanted Result Set
[sexnumbers] => Array
(
[sex] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[no] => Array
(
[0] => 35
[1] => 17
[2] => 3
)
)
I've an associative array called $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[pt_doc_title] => Array
(
[1] => Test Document
[2] => New Joining
[3] => Hallo Jolly
)
[pt_doc_id] => Array
(
[0] => 6
[1] => 7
)
[submit] => Update
)
In order to keep all the package type documents data together I've manipulated the above array as follows:
foreach ($data['pt_doc_title'] as $key => $title) {
$id = isset($data['pt_doc_id'][$key-1]) ? $data['pt_doc_id'][$key-1] : null;
$data['pt_documents_data'][] = array(
'pt_doc_title' => $title,
'pt_doc_id' => $id
);
}
unset($data['pt_doc_title'], $data['pt_doc_id']);
After manipulation I'm getting following array $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
)
)
)
My issue is I'm haivng another array called $_FILES as follows and I want to merge one of it's key(name) into above array in a same manner.
Array
(
[document_file_name_1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_3] => Array
(
[name] => FAQ.doc
[type] => application/msword
[tmp_name] => /tmp/phpFiBYKB
[error] => 0
[size] => 35840
)
)
That is if there exists a value under [name] then the final array should be as follows. As there is a value present only in last array element of array $_FILES
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
[pt_doc_file_iname] =>
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
[pt_doc_file_iname] =>
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
[pt_doc_file_iname] => FAQ.doc
)
)
)
Can anyone please help me in creation of such final array?
Simplest way would be to walk the $_FILES array and extract the id from the last segment of the field name... then use that -1 as the basis for adding the file to your result array.
Something like this should get you there (untested):
foreach($_FILES as $k => $file){
$index_to_update = trim(substr($k, strrpos($k, "_")+1))-1;
$res["pt_document_data"][$index_to_update]["pt_doc_file_iname"] = isset($file["name"])?$file["name"]:"";
}
That is assuming $res is the array that is the parent of the pt_document_data element.